askaale askaale - 23 days ago 7
Swift Question

AppDelegate segue alternative pass data

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.

Answer

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.

enter image description here