0

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
askaale
  • 1,199
  • 1
  • 23
  • 50
  • 1
    You can only `segue` from a `viewController` to another. `AppDelegate` is NOT a viewController and therefore you **can't** segue You should either use delegation pattern or notifications. Or perhaps if you are using the wrong term. You can set your rootViewController – mfaani Nov 13 '16 at 21:07
  • @Honey that's what I thought, thanks for clearing this up. However, could I possibly present the Viewcontroller through storyboard and then pass data? – askaale Nov 13 '16 at 21:09
  • 1
    I recommend seeing [this question](http://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login-screen-handling-clearing-of-data-upon-logou/21889172#21889172) It has a very easy design with good answers. – mfaani Nov 13 '16 at 21:10
  • 1
    There is prepareForSegue, since you have no viewController to prepare from. Yet you can instantiate a viewController and then populate its properties/members and set as the `rootViewController` of your `navigationController` – mfaani Nov 13 '16 at 21:14
  • @Honey Thanks! Do you happen to have any Swift examples? – askaale Nov 13 '16 at 21:16

1 Answers1

2

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

emresancaktar
  • 1,507
  • 17
  • 25