0

I'm trying to dynamically, using code change the Root of my UINavigationController thru his subclass.

Basically, my Storyboard looks like this:

#MARK : App Storyboard

enter image description here

As you can see, I set the the CustomNavigationController as the Initial View Controller option (thru the Storyboard).

How can I, thru the CustomNavigationController class, set up the root ViewController that will be displayed when i'll run the app?

#MARK : 'CustomNavigationController' class

class CustomNavigationController: UINavigationController {

  // What method should i use?

}

1 Answer 1

2

The two view controllers on the right must be given storyboard identifiers (on the identity inspector tab).

Then in your custom NavVC, build the view controller you want at the root, and make it the root by making it the only view controller in the navigation stack (which is an array)...

- (void)viewWillAppear:animated {
    [super viewWillAppear:animated];
    UIStoryboard *storyboard = [self storyboard];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"one of the ids you set up"];
    self.viewControllers = @[ vc ];
}

In swift (pretty sure)...

override func viewWillAppear() {
    super.viewWillAppear()
    let storyboard = self.storyboard
    let vc = storyboard.instantiateViewController(withIdentifier: "one of the ids you set up")
    self.viewControllers = [ vc ] 
}
Sign up to request clarification or add additional context in comments.

5 Comments

ViewWillAppear will get recalled If i'll present and than dismiss a vc. Is there a better func to call this snippet?
viewWillAppear will be called on the navigation vc only when you present it (not vcs on its stack). is that what you mean? can you specify the conditions under which you don't want to do this? if so, we can express those conditions in viewWillAppear
Hey Danh, thank you for responding. Can you elaborate why exactly viewWillAppear is the best place to run this snippet? Is it because you are waiting for 'LayoutSubviews' to kick in? Basically I've divided the 'Permission screen VC' and the 'App flow VC' into two different ViewControllers. So upon load, I'm checking if the user has granted me permission. According to this I'm deciding if to send him to the 'permission VC' or the 'normal app flow VC'.
@RoiMulia, I'm treating viewWillAppear just as a convenient hook that gets run before the view appears. didLayout is okay too, but I think less descriptive of your intent. Maybe I'm misunderstanding, but wouldn't you just use the user permission condition to select the vc identifier? NSString *identifier = (/*user granted permission*/)? @"identifierA" : @"identifierB";
Ya, but don't you have to specify the VC class when calling 'instantiateViewController'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.