0

I have 3 ViewControllers like below.

A: HomeController, B: NavController1, C: NavController2

I set a custom navigation bar in B and C and I use "navigationController?.pushViewController" for the segues From A to B to C.

These are the codes.

Custom navigation bar:

class CustomNavBar: UINavigationBar {

    let navBarView: UIView = {
        let view = UIView()
        view.backgroundColor = .black

        // Logo
        let logo = UIImageView(image: <image-file>)
        view.addSubview(logo)
        logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        logo.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        return view

    }()

    public func setupNavBar() {
        if let window = UIApplication.shared.keyWindow {
            window.addSubview(navBarView)
            navBarView.topAnchor.constraint(equalTo: window.topAnchor, constant: 0).isActive = true
            navBarView.leftAnchor.constraint(equalTo: window.leftAnchor, constant: 0).isActive = true
            navBarView.rightAnchor.constraint(equalTo: window.rightAnchor, constant: 0).isActive = true
        }
    }

} 

And in the ViewControllers B and C I put these codes.

class NavController1: UIViewController {

    let navBar = CustomNavBar()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        navBar.setupNavBar()
    }

    override func viewWillDisappear(_ animated: Bool) {
        loginHeader.headerBg.isHidden = true
        loginHeader.backButton.isHidden = true
    }

}

And for the segues, I used the code below with a UIButton action.

self.navigationController?.pushViewController(NavController1, animated: true)

It works with the segue from A to B but it doesn't work with B to C.

Can someone point out what the problem is? Thank you!

4
  • It is improper to name "pushViewController" a "segue", because it is not. If you want to use segues, you can bind them all in storyboard, and then trigger required segue by it's id: performSegue(withIdentifier: , sender: ) Commented Mar 2, 2018 at 13:55
  • PS: To avoid confusing yourself and others, avoid naming ordinary ViewControllers like NavController, it will suggest that is is derived from UINavigationController (same for all other types, never named view a Button if it is not a UIButton for example etc.) Commented Mar 2, 2018 at 13:59
  • Thank you for the suggestion but I’m not using storyboard. And yes, I’ll name it better for the next time. Commented Mar 2, 2018 at 17:28
  • How NavController1 was created (in both cases)? Did you debugged either self.navigationController not nil (it shouldn't to be it you got to controller B by pushing it, but... ) Commented Mar 5, 2018 at 14:54

1 Answer 1

3

UINavigationController is derived from UIViewController, which means it has a navigationController property. Thing is, this will be nil on the actual UINavigationController because it doesn't have a navigation controller, it is the navigation controller. Because you are using optional chaining, the message is never getting sent because navigationController is nil. Try sending the message to self instead (given that self is of type UINavigationController, and the current one).

I.E. self.pushViewController(NavController1, animated: true)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply! I tried self.pushViewController but the error occured "Value of type 'NavController1' has no member 'pushViewController'".

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.