swift - 如何在appdelegate中调用uiviewcontroller方法

时间:2016-12-21 13:17:50

标签: swift singleton

webView变量为nil。

致命错误:在解包可选值时意外发现nil

如何使用单例对象?

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)
    }
}

1 个答案:

答案 0 :(得分:0)

尝试在_ = viewController.view方法

之后添加instantiateViewController(withIdentifier:)