呈现的UIViewController无法呈现UIAlertController

时间:2018-10-02 13:03:28

标签: ios uiviewcontroller uinavigationcontroller uialertcontroller presentviewcontroller

在我的应用程序中单击“提交”按钮后,SubmitViewController会显示在活动视图控制器上方,以显示上传进度。

收到错误通知后,它应该显示一个带有错误的警报并自行关闭。

问题是因为self.navigationController等于nil,所以没有显示警报。在这种情况下,如何显示警报?

我不能像某些人所建议的那样使用情节提要实例化SubmitViewController,因为它不是情节提要的一部分。

用于显示SubmitViewController的视图控制器:

-(void)submitBtnClicked:(id)sender {
    SubmitViewController *submitViewController = [SubmitViewController new];
    [self.navigationController presentViewController:submitViewController animated:YES completion:nil];
}

SubmitViewController.swift

@objc func submitErrorNotification(_ notification:Notification) {
    self.unsubscribe()

    let title:String = notification.userInfo!["title"] as! String
    let message:String = notification.userInfo!["message"] as! String

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

    if (self.navigationController == nil) {
        NSLog("Error: navigation controller is nil");// THIS error occurs
    }

    self.navigationController?.present(alertController, animated: false, completion: nil)

    self.dismiss(animated: false, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

为什么在中使用警告关键字:

[self.navigationController presentViewController:alert animated:YES completion:nil];

尝试一下:

SubmitViewController *myViewController = [SubmitViewController new];
    [self.navigationController presentViewController:myViewController animated:YES completion:nil];