Home > Back-end >  Missing transition from first view controller to second view controller using navigation controller
Missing transition from first view controller to second view controller using navigation controller

Time:04-19

I want to create a navigation hierarchy where I want to go to a SecondViewController from FirstViewController using a NavigationController. The FirstViewController contains a button B that I intend to use to go to a SecondViewController. I am employing the navigation controller concept since I want to return to the FirstViewController later. I have done the following steps to achieve it:

  1. I have embedded a NavigationController into a storyboard X containing both the FirstViewController and SecondViewController.
  2. I have created a segue from the button B present in the FirstView (has FirstViewController associated with it) to the SecondView (has SecondViewController).
  3. I set the navigationViewController nvc as following after I have presented the firstViewController:
nvc = UINavigationController.init(rootViewController: firstViewController)
  1. Later on, when the button B gets pressed, I execute the following code to push the secondViewController onto the navigation hierarchy:
self.nvc?.pushViewController(secondViewController, animated: true)

However, the secondView doesn't present itself even after pushing.

  1. I have named the segue between the button B and secondViewController as kSegue, so I tried to perform the segue as an addition to see if the secondViewController presents itself or not:
self.performSegue(withIdentifier: "kSegue", sender: self)

An exception occurs when both the 4th and 5th steps are performed together. The exception states that I'm pushing a viewController that already exists on the navigation hierarchy, but the second view still doesn't open even if I comment the performSegue code.

May I ask what mistake I am making here?

CodePudding user response:

In the storyboard, make sure there is a rootViewController segue from the navigation controller to the first view controller. Also, make sure the navigation controller is marked as the initial view controller.

In the code, change

self.nvc?.pushViewController...

To

self.navigationController?.pushViewController...

CodePudding user response:

1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller 
3) set view controller A as a root view controller of your navigation controller

4) in "GoToB" method access View controller B's instance and push it in navigation controller 

5) On View controller B's "Go Back" method write code to pop it.

6) Dont forget to set storyboard Id on both A & B view controller

enter image description here

class FirstViewController: UIViewController
    {
        
                lazy var secondViewController : SecondViewController? =
                {
                    let secondViewController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
                    return secondViewController
                }()
                
                override func viewDidLoad()
                {
                    super.viewDidLoad()
                }
                
                @IBAction func goToB(sender : UIButton)
                {
                    guard let secondViewController = self.secondViewController else
                    {
                        return
                    }
                    
                    self.navigationController?.pushViewController(secondViewController, animated: true)
                }
            }
    
    
    
    class SecondViewController: UIViewController 
    {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
        @IBAction func goBack(sender : UIButton)
        {
            self.navigationController?.popViewController(animated: true)
        }
    }
  • Related