user5700760
user5700760

Reputation:

Get ViewController from Appdelegate

I have four views in my storyboard

  1. ViewOne
  2. ViewTwo
  3. ViewThree
  4. ViewFour

In my ViewTwo, I make a call and open an new App and when the URL opening is done I call the following function in my Appdelegate

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool

What I do here is that I iterate through all my viewControllers because I want to find my ViewTwo to be able to call a segue that I have created. I want to do it this way because I don´t want to create a new instance of the viewController.

It works great with this function below to find ViewTwo:

if let viewControllers = window?.rootViewController?.childViewControllers {
    for viewController in viewControllers {
        if viewController.isKindOfClass(ViewTwo) {
            viewController.performSegueWithIdentifier("sw", sender: self)
        }
    }
}

But now I have added a NavigationController to ViewTwo, ViewThree and ViewFour so when I run the snippet above I only get the following result for viewController (I made a simple print(viewController))

<ViewOne: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>
<UINavigationController: 0x...>

So my question is, how do I check for ViewTwo now that I also have a NavigationController?

Upvotes: 1

Views: 2866

Answers (3)

user5700760
user5700760

Reputation:

I solved this by

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("ViewThree") as! WebViewController
self.window?.rootViewController!.presentViewController(controller, animated: true, completion: { () -> Void in

})

Upvotes: 2

Lucas Huang
Lucas Huang

Reputation: 4016

Because now you are all getting UINavigationViewController. And it's just simply a controller of controller. You can access the root view controller from UINavigationViewController by accessing the first element of viewControllers property. Then, you are able to make the interpolation.

The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Upvotes: 0

brl214
brl214

Reputation: 537

You can get the view controller from storyboard without creating a new instance. You don't need to iterate through all the view controllers. Also why do you not set the view controller as the root of your nav controller instead of performing a segue?

You can get the view controller from storyboard like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("ViewTwo") as! ViewTwoViewController

Upvotes: 0

Related Questions