2

I want to change the root controller of my navigation controller programatically:

import UIKit
import Foundation

class NavigationController : UINavigationController {
    override init(rootViewController : UIViewController) {
        print("TEST")
        super.init(rootViewController : rootViewController)
    }

    override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
        print("TEST2")
        super.init(navigationBarClass : navigationBarClass, toolbarClass : toolbarClass)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

I set the NavigationController class as custom class of my Navigation Controller in the storyboard

The console does not show my test output. What am I doing wrong and how can I change the root controller here?

Simon Hessner
  • 1,757
  • 1
  • 22
  • 49

2 Answers2

1

If you instantiate your custom NavigationController from Storyboard you are calling this method:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

To see your print in console you have to init it programmatically, for example:

let navVC = NavigationController(rootViewController: yourVC)
Francesco Deliro
  • 3,899
  • 2
  • 19
  • 24
  • You are right, I forgot the print in the required method. But how can I change the rootViewController there? – Simon Hessner Nov 20 '17 at 21:48
  • Got it: See my answer below – Simon Hessner Nov 20 '17 at 22:03
  • iOS development is so strange... Thanks for your help! – Simon Hessner Nov 20 '17 at 22:07
  • 1
    You are welcome ;) but checking your answer if you init your navigation controller from storyboard you can set there the root controller, it’s not the correct to set it in init coder. And if you want to change it subsequently you can update the navigation controller view controllers array. – Francesco Deliro Nov 20 '17 at 22:12
  • If I call setViewControllers in my viewController in viewDidLoad instead of in my custom ViewController, the "content view" is changed correctly, but the header of the navigation controller is not updated. So I think this is not the right place to do it? Where would you set the rootViewController programmatically if the user can decide in the settings which view he wants to use? – Simon Hessner Nov 27 '17 at 10:28
  • You can change it in your Settings section and then call the popToRootViewController to show the new root. – Francesco Deliro Nov 27 '17 at 11:00
0

Got it:

import UIKit
import Foundation

class NavigationController : UINavigationController {
    override init(rootViewController : UIViewController) {
        super.init(rootViewController : rootViewController)
    }

    override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
        super.init(navigationBarClass : navigationBarClass, toolbarClass : toolbarClass)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "new_main_view") as! ViewController
        self.setViewControllers([newViewController], animated: false)
    }
}

Is this the recommended way to provide the user two "themes" of the same view?

Simon Hessner
  • 1,757
  • 1
  • 22
  • 49