[英]Call ViewControllers Method or Segue from App Delegate (Swift)
我創建了一個使用情節提要的應用程序,並成功創建了一個可以正常工作的表格視圖和詳細信息頁面。
我希望這樣做,以便將刷了localNotifications的用戶發送到應用程序中正確的詳細信息頁面。
看來我可以從ViewController調用函數,但是只要它們引用自身以更新任何詳細信息或執行測試,應用程序就會崩潰。
我在ViewController中擁有的代碼如下:
func handleMessageNotification (messageID:String)
{
// this function should act as a conduit for the call from the delegate
println("notif messageID: \(messageID)");
self.performSegueWithIdentifier("showMessageDetail", sender: self);
self.messageView.reloadData();
}
這是從我的appDelegate調用的
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if (application.applicationState == UIApplicationState.Background || application.applicationState == UIApplicationState.Inactive)
{
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ViewController") as ViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)
setViewController.handleMessageNotification(messageID);
}
}
println可以正常工作,但是performSegue失敗(致命錯誤:在展開Optional值時意外發現nil)並且messageView.reload()失敗(相同錯誤)。
我如何獲得通知,以在打開應用后將其啟動到正確的位置?
此解決方案“ 在Swift中從AppDelegate獲取ViewController實例 ”使用了許多相同的解決方案,但我的解決方案不允許使用ViewController訪問任何內容。
=======更新-解決方案=======
對於任何其他有此問題的人。 根據加布(Gabuh)的建議: 對我來說,完整的解決方案是執行以下操作:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if (application.applicationState == UIApplicationState.Background || application.applicationState == UIApplicationState.Inactive)
{
let navigationController = application.windows[0].rootViewController as UINavigationController;
navigationController.popToRootViewControllerAnimated(false); // I need to push back to root before performing segue
let rootViewController = navigationController.visibleViewController;
rootViewController.performSegueWithIdentifier("showMessageDetail", sender: self);
}
}
這也允許我在視圖中調用函數,例如在原始示例中
rootViewController.handleMessageNotificaion(messageID);
要么
rootViewController.messageView.reloadData();
您正在創建一個新ViewController
的實例。 並且您試圖在該ViewController上調用一個函數,即試圖在甚至不顯示ViewController時執行segue。 我認為那是行不通的...
如果要從詳細視圖執行序列,則必須訪問當前的實例化視圖Controller並從中執行序列。
如果您想知道如何獲取正在顯示的UIViewController的實例,那么在這篇文章中,有幾種方法向您展示如何實現:
用偽代碼在您的appDelegate中:
1-獲取當前的UIViewController實例(不是新實例)
2-從該視圖控制器執行perfom segue(或以模態呈現或您想要的任何過渡)
在Swift 3中
guard let rvc = self.window?.rootViewController as? VCName else { return }
rvc.methodInYourVC()
以上假設
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.