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?

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105

1 Answers1

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 ] 
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • ViewWillAppear will get recalled If i'll present and than dismiss a vc. Is there a better func to call this snippet? – Roi Mulia Jul 14 '17 at 14:35
  • 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 – danh Jul 14 '17 at 14:40
  • 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'. – Roi Mulia Jul 14 '17 at 14:48
  • @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";` – danh Jul 14 '17 at 14:59
  • Ya, but don't you have to specify the VC class when calling 'instantiateViewController'? – Roi Mulia Jul 14 '17 at 15:25