简体   繁体   English

iOS5到iOS4崩溃-ViewControllers

[英]iOS5 to iOS4 crash - ViewControllers

Currently I am trying to make my app available for iOS4, to increase the number of people we can reach. 目前,我正在尝试使我的应用程序可用于iOS4,以增加我们可以覆盖的人数。

We have moved away from StoryBoard and are now using .xib files instead. 我们已经从StoryBoard移开了,现在正在使用.xib文件。

Right now, the only thing that is stopping this is this error: 现在,唯一阻止此错误的是此错误:

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) 

Usually I use this to open another view: 通常我用它来打开另一个视图:

    UIViewController *controller;
    controller = [[CategoryViewController alloc]initWithNibName:@"CategoryViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:controller animated:YES];
    [controller release];

But in this case I call a function from a different file (for efficiency) using this code: 但是在这种情况下,我使用以下代码从其他文件中调用函数(以提高效率):

#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]; 

}

How can I fix this? 我怎样才能解决这个问题? Thanks in advance! 提前致谢!

presentViewController:animated:completion: is available on iOS5 and above only, hence you get that crash. presentViewController:animated:completion:仅在iOS5及更高版本上可用,因此会导致崩溃。

Change the last part to: 将最后一部分更改为:

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]; 

This will first check if the new way of presenting a viewController is supported. 这将首先检查是否支持呈现viewController的新方法。 For making sure that this works fine in the future, as presentModalViewController: is marked as being deprecated, we only use that option if the new way is not available. 为了确保将来能正常工作,因为presentModalViewController:被标记为已弃用,我们仅在新方法不可用时才使用该选项。

You may need to present a navcontroller, and/or use presentModalViewController, try the following: 您可能需要提供一个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];

The Till's solution is far the best. Till的解决方案是最好的。
But if, like me, you have numbers of present/DismissModalViewController: calls in your code, and doesn't want to duplicate the respondsToSelector test and is deprecated warnings over and over, just make UIViewController addition: 但是,如果像我一样,你的存在/ DismissModalViewController号码:电话在你的代码,并没有要复制的respondsToSelector测试并is deprecated一遍又一遍的警告,只是做的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM