2017-09-04 50 views
-1

我用我的AppDelegate以下彈出警報用戶在收到推送:接收錯誤:類型的UIViewController「的價值還沒有成員「openViewControllerBasedOnIdentifier」

let topWindow = UIWindow(frame: UIScreen.main.bounds) 
    topWindow.rootViewController = UIViewController() 
    topWindow.windowLevel = UIWindowLevelAlert + 1 
    let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert) 
    let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
     print("you pressed Yes, please button") 
     topWindow.isHidden = true 
     topWindow.rootViewController?.openViewControllerBasedOnIdentifier("consumerProfile") 
    }) 
    let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
     print("you pressed No, thanks button") 
     topWindow.isHidden = true 
    }) 
    alert.addAction(yesButton) 
    alert.addAction(noButton) 

    topWindow.makeKeyAndVisible() 
    topWindow.rootViewController?.present(alert, animated: true, completion: { _ in }) 

我的問題是:

topWindow.rootViewController?.openViewControllerBasedOnIdentifier("consumerProfile") 

功能openViewControllerBasedOnIdentifier僅適用於基於名爲BaseViewController這樣一個特定的UIViewController類的意見存在,我收到以下錯誤:

Value of type 'UIViewController' has no member 'openViewControllerBasedOnIdentifier'

+0

你有什麼問題?您無法提供來自AppDelegate的提醒或您發佈的錯誤? –

+1

你只需將你的'rootViewController'設置爲一個空的'UIViewController'。你如何期待能夠在這個'UIViewController'上調用你自定義的'BaseViewController'的方法? – rmaddy

+0

你見過這個:https://stackoverflow.com/questions/30592521/opening-view-controller-from-app-delegate-using-swift –

回答

0

只要把你的自定義類用作RootViewController的

let topWindow = UIWindow(frame: UIScreen.main.bounds) 
let topViewController = BaseViewController() 
topWindow.rootViewController = topViewController 
topWindow.windowLevel = UIWindowLevelAlert + 1 
let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert) 
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("you pressed Yes, please button") 
    topWindow.isHidden = true 
    topViewController.openViewControllerBasedOnIdentifier("consumerProfile") 
}) 
let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("you pressed No, thanks button") 
    topWindow.isHidden = true 
}) 
alert.addAction(yesButton) 
alert.addAction(noButton) 

topWindow.makeKeyAndVisible() 
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in }) 

UPDATE

因爲你的ViewController沒有init()試試這個代碼:

let topWindow = UIWindow(frame: UIScreen.main.bounds) 
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Name of storyboard should be same as your 
let topViewController = storyboard.instantiateViewController(withIdentifier: identifier) as? BaseViewController // be sure that `identifier` of BaseViewController is same as in storyboard 
topWindow.rootViewController = topViewController 
topWindow.windowLevel = UIWindowLevelAlert + 1 
let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert) 
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("you pressed Yes, please button") 
    topWindow.isHidden = true 
    topViewController.openViewControllerBasedOnIdentifier("consumerProfile") 
}) 
let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in 
    print("you pressed No, thanks button") 
    topWindow.isHidden = true 
}) 
alert.addAction(yesButton) 
alert.addAction(noButton) 

topWindow.makeKeyAndVisible() 
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in }) 

更新2

還有就是你identifier = storyboard ID

enter image description here

+1

這是如何改變'rootViewController'? – rmaddy

+0

@maddy忘了更改topWindow.rootViewController = topViewController –

+0

@YerkebulanAbildin無法實例化視圖控制器,如果我想要菜單結構,則需要使用openViewControllerBasedOnIdentifier方法。當我嘗試頂部的應用程序崩潰與「致命錯誤:意外地發現零,而解包可選值」 –

相關問題