【问题标题】:performSegue error when come from appDelegate来自 appDelegate 时的 performSegue 错误
【发布时间】: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


【解决方案1】:

AppDelegate 类你不能执行segue。所以这条线永远不会在你的 AppDelegate 类中起作用。

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 为您的控制器提供标识符,请按照以下步骤操作。

  • 打开主故事板
  • 点击您要打开的视图
  • 从控制器顶部选择黄色/向左按钮
  • 现在打开位于右侧的实用工具箱
  • 现在点击Show the Identical Inspector
  • 现在您可以在此处将您的身份作为 StoryBoard Id 提供

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多