【发布时间】:2017-06-07 23:02:11
【问题描述】:
我的情况很特别,因为如果应用程序在正常模式下打开,我可以正常工作。
这是我在收到推送通知后尝试这样做的时候。
内部 didReceive 通知:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
我正在从我的initial view controller(HOME) 调用一个函数
这只是点击通知时应用打开时显示的视图
let homeVC : HomeViewController! = HomeViewController()
homeVC.goAdvertisement(id, data: dataInfo)
在goAdvertisement内你可以找到:
self.performSegue(withIdentifier: "mySegue", sender: itemData)
这就是错误发生的地方。
libc++abi.dylib: terminating with uncaught exception of type NSException
起初我认为可能是它给了加载视图的时间,以便我可以执行performSegue,并尝试使用以下方法给它一个延迟:
func delay(delay:Double, closure:@escaping ()->()) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
closure()
}
}
self.delay(delay: 4, closure: {
let homeVC : HomeViewController! = HomeViewController()
homeVC.goAdvertisement(id, hardwareAd: dataInfo)
}
但它不起作用
我的另一个想法是将performSegue 放在一边,尝试使用present。
let vc = self.storyboard?.instantiateViewController(withIdentifier: "myVcId") as! MyController
vc.data = itemData
self.present(vc, animated: true, completion: nil)
但这也不起作用(崩溃指向instantiateViewController 行)
任何帮助将不胜感激,谢谢。
更新
关注APK APPS 的回答。终于找到方法了。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "homeView") as! HomeViewController
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(homeVC, animated: false);
【问题讨论】:
-
你不能只创建一个视图控制器的实例并要求它执行segue;您需要获取已经在屏幕上的实例并要求它执行 segue。
-
获取实例还不够,还是失败了。我用最终解决方案更新我的问题。
标签: ios swift crash segue uistoryboardsegue