来自 appDelegate 时的 performSegue 错误
performSegue error when come from appDelegate
我的情况比较特殊,因为如果在正常模式下打开应用程序,这对我来说是正确的。
这是我在收到推送通知后尝试这样做的时候。
内部 didReceive 通知:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
我正在从我的 initial view controller(HOME)
中调用一个函数
That is just the view that is displayed when the application opens when you click on the notification
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);
From the AppDelegate
class you can not perform segue
. So this line will never work from your AppDelegate
class.
self.performSegue(withIdentifier: "mySegue", sender: itemData)
你需要简单
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let scheduleController = storyboard.instantiateViewController(withIdentifier: "yourIdentiFier")
self.window!.rootViewController = scheduleController
self.window!.makeKeyAndVisible()
您可以从您的 Main.storyboard
向您的控制器提供标识符,请按照以下步骤操作。
- 打开主故事板
- 单击要打开的视图
- Select 该控制器顶部的黄色/左按钮
- 现在打开位于右侧的实用工具箱
- 现在点击
Show the Identical Inspector
- 现在您可以将您的身份作为 StoryBoard Id
我的情况比较特殊,因为如果在正常模式下打开应用程序,这对我来说是正确的。
这是我在收到推送通知后尝试这样做的时候。
内部 didReceive 通知:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
我正在从我的 initial view controller(HOME)
That is just the view that is displayed when the application opens when you click on the notification
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);
From the
AppDelegate
class you can not performsegue
. So this line will never work from yourAppDelegate
class.
self.performSegue(withIdentifier: "mySegue", sender: itemData)
你需要简单
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let scheduleController = storyboard.instantiateViewController(withIdentifier: "yourIdentiFier")
self.window!.rootViewController = scheduleController
self.window!.makeKeyAndVisible()
您可以从您的 Main.storyboard
向您的控制器提供标识符,请按照以下步骤操作。
- 打开主故事板
- 单击要打开的视图
- Select 该控制器顶部的黄色/左按钮
- 现在打开位于右侧的实用工具箱
- 现在点击
Show the Identical Inspector
- 现在您可以将您的身份作为 StoryBoard Id