Search code examples
iosswiftsegueuistoryboardsegue

push is not working on the controller which was presented modally


Hello I have a button on FirstViewController and I dragged a segue from firstViewController to secondViewController as segue Modally. Now on the secondViewController I am generating a Button Programmatically and to go on the third controller I am pushing the controller(segue Push) But its not working. thirdcontroller doesn't comes up. Nothing happening when I click the custom button.

Here is the secondViewController code

 func nextButtonClicked(sender:UIButton!){
    let takeProductPhotoController = self.storyboard!.instantiateViewControllerWithIdentifier("takeProductPhotoController") as! TakeProductPhotoController
            takeProductPhotoController.trip = trip
            self.navigationController?.pushViewController(takeProductPhotoController, animated: true)
}

Note: I don't need the NavigationBar on Second View Controller so while presenting it modally it didn't show the navigationBar. But I need the navigationBar on the third one. So thats why I am pushing in naviagtioncontroller


Solution

    • Whenever you want push from the popped controller you have to creat the new NavigationController & also set the ViewController which you want to present modaly as rootViewController of navigationController.
    • When we want to present the SecondViewcontrollermodally, We are presenting navigation controller with rootViewController set to SecondViewController. So we have created the new navigation stack. After that we are going to push the controller in the new navigation stack created while presenting SecondViewController.

    1) Your code should be look like this in FirstViewController

    let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("aIdentifier") as! SecondViewController 
        let nav = UINavigationController(rootViewController: SecondViewController )
        self.presentViewController(nav, animated:true, completion:nil)
    

    2) From secondViewController you can push ThirdViewController in self.naviagtionController. Write below code in SecondViewController on Button Action

    let thirdViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("cIdentifier") as! ThirdViewController self.navigationController?.pushViewController(thirdViewController , animated: true)}