【问题标题】:"Application tried to present a nil modal view controller on target" Error on iOS7 and below“应用程序试图在目标上呈现一个 nil 模式视图控制器”iOS7 及以下错误
【发布时间】:2016-12-22 09:28:02
【问题描述】:

您好,我正在迁移旧的 iOS 项目,我从帮助类 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] *** 由于以下原因而终止应用程序 未捕获的异常“NSInvalidArgumentException”,原因:“应用程序 试图在目标上呈现一个 nil 模态视图控制器 .'

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

【问题讨论】:

    标签: ios objective-c ios7 uialertcontroller


    【解决方案1】:

    根据 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];
        }
    

    【讨论】:

      【解决方案2】:

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

      适用于 iOS 8.0 及更高版本

      对于 iOS 7,您需要使用 UIAlertView

      【讨论】:

      • 感谢您的信息。我们现在是否仍支持 IOS 7。我应该将开发目标移至 8.0 吗?
      • 如果你停止支持 iOS 7 用户,我认为是的,你可以将开发目标更改为 8.0
      • 我的意思是我们应该保留代码以支持 IOS7 还是完全停止支持(将目标更改为 8.0)? 2016 年 8 月?使用 iOS 7 的用户多吗?
      • 这只是您的选择。我不知道你的应用中有多少用户使用 iOS 7。您需要为此查看您的应用程序分析。它适用于不同的应用程序。你可以在iTunes中查看它在ios7中连接了多少用户
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2017-01-25
      • 2013-01-06
      • 2014-09-16
      相关资源
      最近更新 更多