Iphone iOS5到iOS4崩溃-视图控制器

Iphone iOS5到iOS4崩溃-视图控制器,iphone,objective-c,xcode,ios5,ios4,Iphone,Objective C,Xcode,Ios5,Ios4,目前,我正在尝试将我的应用程序用于iOS4,以增加我们可以联系到的人数 我们已经不再使用故事板,而是使用.xib文件 现在,唯一能阻止这种情况的是这个错误: 2012-09-14 18:32:42.705 CrunchCalculator[7037:11303] -[CategoryViewController presentViewController:animated:completion:]: unrecognized selector sent to instance 0x6b42230

目前,我正在尝试将我的应用程序用于iOS4,以增加我们可以联系到的人数

我们已经不再使用故事板,而是使用.xib文件

现在,唯一能阻止这种情况的是这个错误:

2012-09-14 18:32:42.705 CrunchCalculator[7037:11303] -[CategoryViewController presentViewController:animated:completion:]: unrecognized selector sent to instance 0x6b42230
2012-09-14 18:32:42.707 CrunchCalculator[7037:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CategoryViewController presentViewController:animated:completion:]: unrecognized selector sent to instance 0x6b42230'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013625a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00ef1313 objc_exception_throw + 44
    2   CoreFoundation                      0x013640bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x012d3966 ___forwarding___ + 966
    4   CoreFoundation                      0x012d3522 _CF_forwarding_prep_0 + 50
    5   CrunchCalculator                    0x0002e560 +[OpenCalcViewController openCalcView:fromViewController:] + 8256
    6   CrunchCalculator                    0x0002fe0a -[CategoryViewController openCalc:] + 106
    7   UIKit                               0x002554fd -[UIApplication sendAction:to:from:forEvent:] + 119
    8   UIKit                               0x002e5799 -[UIControl sendAction:to:forEvent:] + 67
    9   UIKit                               0x002e7c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    10  UIKit                               0x002e67d8 -[UIControl touchesEnded:withEvent:] + 458
    11  UIKit                               0x004e84de _UIGestureRecognizerSortAndSendDelayedTouches + 3609
    12  UIKit                               0x004e8c53 _UIGestureRecognizerUpdateObserver + 927
    13  CoreFoundation                      0x0134389b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    14  CoreFoundation                      0x012d86e7 __CFRunLoopDoObservers + 295
    15  CoreFoundation                      0x012a11d7 __CFRunLoopRun + 1575
    16  CoreFoundation                      0x012a0840 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x012a0761 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x014b61c4 GSEventRunModal + 217
    19  GraphicsServices                    0x014b6289 GSEventRun + 115
    20  UIKit                               0x00263c93 UIApplicationMain + 1160
    21  CrunchCalculator                    0x0000276a main + 170
    22  CrunchCalculator                    0x000026b5 start + 53
)
terminate called throwing an exception(lldb) 
通常我使用此选项打开另一个视图:

    UIViewController *controller;
    controller = [[CategoryViewController alloc]initWithNibName:@"CategoryViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:controller animated:YES];
    [controller release];
但在本例中,我使用以下代码从不同的文件调用函数(为了提高效率):

#import "AllofTheFiles.h"

+ (void)openCalcView: (NSString *)nameOfView fromViewController:(UIViewController *)controller {

    NSUserDefaults *saveSpot = [NSUserDefaults standardUserDefaults];

    UIViewController *modalController;

    if ([nameOfView isEqualToString:@"Tax"]) {

        modalController= [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil];

    }else if ([nameOfView isEqualToString:@"Rent"]){

        modalController= [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil];

    }  //... cut other else if's to save space

modalController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[controller presentViewController:modalController animated:YES completion:nil];
[modalController release]; 

}

我怎样才能解决这个问题?提前谢谢

presentViewController:animated:completion:
仅在iOS5及更高版本上可用,因此会发生崩溃

将最后一部分更改为:

modalController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//do we support the new way of presenting a modal viewController?
if ([controller respondsToSelector:@selector(presentViewController:animated:completion:)])
{   //yes->so do it!
    [controller presentViewController:modalController animated:YES completion:NULL];
}
else
{   //nope->we seem to be running on something prior to iOS5, do it the old way!
    [controller presentModalViewController:modalController animated:YES];
}
[modalController release]; 

这将首先检查是否支持显示viewController的新方式。为了确保这在将来可以正常工作,因为
presentModalViewController:
被标记为已弃用,我们仅在新方法不可用时使用该选项

presentViewController:animated:completion:
仅在iOS5及更高版本上可用,因此会发生崩溃

将最后一部分更改为:

modalController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//do we support the new way of presenting a modal viewController?
if ([controller respondsToSelector:@selector(presentViewController:animated:completion:)])
{   //yes->so do it!
    [controller presentViewController:modalController animated:YES completion:NULL];
}
else
{   //nope->we seem to be running on something prior to iOS5, do it the old way!
    [controller presentModalViewController:modalController animated:YES];
}
[modalController release]; 

这将首先检查是否支持显示viewController的新方式。为了确保这在将来可以正常工作,因为
presentModalViewController:
被标记为已弃用,我们仅在新方法不可用时使用该选项

您可能需要演示navcontroller和/或使用presentModalViewController,请尝试以下操作:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:modalController];
navController.modalPresentationStyle = UIModalPresentationPageSheet;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[controller  presentModalViewController:navController animated:YES completion:nil];
[modalController release];
[navController release];

您可能需要演示navcontroller和/或使用presentModalViewController,请尝试以下操作:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:modalController];
navController.modalPresentationStyle = UIModalPresentationPageSheet;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[controller  presentModalViewController:navController animated:YES completion:nil];
[modalController release];
[navController release];

蒂尔的解决方案是最好的。
但是,如果像我一样,您的代码中有大量的present/DismissModalViewController:调用,并且不想重复
respondsToSelector
测试和
被弃用
警告,只需添加UIViewController:

@implementation UIViewController (iOS4Compatibility)

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag {
    if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
        [self presentViewController:viewControllerToPresent animated:flag completion:nil];
    else
        [self presentModalViewController:viewControllerToPresent animated:flag];
}

- (void)dismissViewControllerAnimated:(BOOL)flag {
    if([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
        [self dismissViewControllerAnimated:flag completion:nil];
    else
        [self dismissModalViewControllerAnimated:flag];
}

@end

蒂尔的解决方案是最好的。
但是,如果像我一样,您的代码中有大量的present/DismissModalViewController:调用,并且不想重复
respondsToSelector
测试和
被弃用
警告,只需添加UIViewController:

@implementation UIViewController (iOS4Compatibility)

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag {
    if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
        [self presentViewController:viewControllerToPresent animated:flag completion:nil];
    else
        [self presentModalViewController:viewControllerToPresent animated:flag];
}

- (void)dismissViewControllerAnimated:(BOOL)flag {
    if([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])
        [self dismissViewControllerAnimated:flag completion:nil];
    else
        [self dismissModalViewControllerAnimated:flag];
}

@end

您确定CatagoryViewController是UIViewController类型吗?您确定CatagoryViewController是UIViewController类型吗?非常感谢。这两者之间的区别是什么?除了明显的(完成块处理程序)之外,两者几乎相同。更多信息,请查看文档。它不适用于我。我无法理解为什么,但即使在4.3中,[self-respondsToSelector:@selector(presentViewController:animated:completion:)]始终返回YES@马丁再次检查-你似乎得到了错误的结果。哼。。。看来你的权利。。。不过我已经仔细检查过了,有时候三重检查是受欢迎的。非常感谢。这两者之间的区别是什么?除了明显的(完成块处理程序)之外,两者几乎相同。更多信息,请查看文档。它不适用于我。我无法理解为什么,但即使在4.3中,[self-respondsToSelector:@selector(presentViewController:animated:completion:)]始终返回YES@马丁再次检查-你似乎得到了错误的结果。哼。。。看来你的权利。。。不过我已经仔细检查过了,有时候三重检查是受欢迎的。