【发布时间】:2018-10-02 13:03:27
【问题描述】:
当在我的应用程序中单击提交按钮时,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)
}
【问题讨论】:
-
为什么要在导航控制器上呈现警报控制器?为什么不直接在
SubmitViewController实例上显示它并在操作处理程序中关闭它 -
@Paulw11 这是我现在的解决方案。我需要在导航控制器上显示它,以便我可以关闭底层视图控制器并将其替换为不同的视图控制器。
-
那么您可能应该使用委托模式,以便提交视图控制器可以告诉委托人有错误并且委托人可以决定做什么
标签: ios uiviewcontroller uinavigationcontroller uialertcontroller presentviewcontroller