2016-12-21 29 views
-1

webView變量爲零。swift - 如何在appdelegate中調用uiviewcontroller方法

致命錯誤:意外發現零而展開的可選值

如何使用單獨的對象?

class PushHandler: NSObject, UAPushNotificationDelegate { 

    func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: @escaping() -> Swift.Void) { 
     print("The user selected the following action identifier:%@", notificationResponse.actionIdentifier); 

     if let targetUrl = notificationResponse.notificationContent.notificationInfo["url"] { 
      let viewController:MCKWebViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MCKWebViewController") as! MCKWebViewController 
      // .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it 

      self.window?.rootViewController?.present(viewController, animated: false, completion: nil) 
      viewController.goURL(targetUrl as! String) 
     } 
     completionHandler() 
} 



class MCKWebViewController: UIViewController { 

    @IBOutlet weak var webView: UIWebView! 

    func goURL(_ targetUrl: String) { 
     let url = URL(string: targetUrl) 
     let urlRequest = URLRequest(url: url!) 
     webView.loadRequest(urlRequest) 
    } 
} 
+0

問題是什麼加_ = viewController.view?你的錯誤發生在哪裏? – Mingebag

+0

webView.loadRequest(urlRequest) webView is nil。 致命錯誤:在解包可選值時意外發現nil –

回答

0

嘗試後instantiateViewController(withIdentifier:)方法

相關問題