AppDelegate segue alternative pass data

为君一笑 提交于 2020-01-03 05:06:05

问题


I am struggling with passing data from my appDelegate to one of my view controllers. I have tried to use segues to do this, but this just doesn't work. So what I am wondering about is; is there any other way to present another view controller and pass data to it, other than using segues?

Thanks in advance.


回答1:


You can't use segue to ViewController from AppDelegate, because AppDelegate is not a ViewController. Segues are only work with ViewControllers.

But you can initiate a storyboard identifier from your AppDelegate to your ViewController so you can send data.

There is a example for you;

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let yourViewController = storyboard.instantiateViewController(withIdentifier: "yourViewControllerStoryboardIdentifier") as! YourViewControllerClassName
        yourViewController.yourProperty = "yourValue"
        self.window?.rootViewController = yourViewController
        self.window?.makeKeyAndVisible()
        return true
    }

You can set your Storyboard ID in your Main.storyboard. Just select your ViewController and set storyboard ID section click the Use Storyboard ID checkmark like image.



来源:https://stackoverflow.com/questions/40578791/appdelegate-segue-alternative-pass-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!