首页 > 解决方案 > 在 AppDelegate 中推送时不显示新的 ViewController

问题描述

这是我在 swift 5 中从应用程序委托中呈现 ViewController 的代码,我已经完成了与everyOne 相同的所有操作,但是在启动应用程序时不会显示新的 ViewController。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let welcome  = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = welcome
        self.window?.makeKeyAndVisible()
 return true
}

标签: iosswiftappdelegateswift5

解决方案


把它放进去

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let welcome  = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = welcome
        self.window?.makeKeyAndVisible()

        return true 
}

更新: 如果它不工作尝试:

 if let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main){
      if let welcome  = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen{
           //Does it come here?
           self.window = UIWindow(frame: UIScreen.main.bounds)
           self.window?.rootViewController = welcome
           self.window?.makeKeyAndVisible()        
     }
 }

确保你真正进入身体,如果最内在if的是if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen


推荐阅读