"Application tried to present a nil modal view controller on target" iOS7 及以下错误

"Application tried to present a nil modal view controller on target" Error on iOS7 and below

您好,我正在迁移旧的 iOS 项目,我从助手 class 的新代码行中收到此错误 ApiManager.m(网络调用)

UIAlertController *alert = [UIAlertController alertControllerWithTitle:NO_INTERNET_ERROR_TITLE message:TRACKED_ITEM_NOT_FOUND_ERROR preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];

错误是:

016-08-16 13:52:49.955 SingPost[967:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'

该问题仅出现在 iOS 7 及以下版本。从版本 8 及以上没有错。如何解决这个问题?非常感谢任何帮助。谢谢!

问题发生在 IOS 7 因为 UIAlertController 正如文档所说 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

Available in iOS 8.0 and late

对于iOS 7 你需要使用UIAlertView

根据 iOS 版本,您应该使用 UIAlertView 和 UIAlertController:

if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
 {
   alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Invalid Coupon."  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
 }
        else
       {
        UIAlertController *alertController = [UIAlertController    alertControllerWithTitle:@"Warning" message:@"Invalid Coupon." preferredStyle:UIAlertControllerStyleAlert];


        UIAlertAction *okAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action)
                                   {

                                   }];

        [alertController addAction:okAction];

        [self presentViewController:alertController animated:YES completion:nil];
    }