1

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

hellosheikh
  • 2,929
  • 8
  • 49
  • 115

2 Answers2

3
  • 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)}
Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29
  • okay you mean I have to write this code.. let nav : UINavigationController = UINavigationController(rootViewController: self.storyboard!.instantiateViewControllerWithIdentifier("takeProductPhotoController") as! TakeProductPhotoController) self.navigationController?.pushViewController(nav, animated: true) ? – hellosheikh Feb 12 '16 at 19:45
  • Yes that's what I mean,but present navigation controller instead of push@ hellowsheikh – Rohit Pradhan Feb 12 '16 at 19:59
  • could you provide me the exact syntax since I am struggling to make it work.. I have tried this var rootViewController = self.window.rootViewController as UINavigationController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) var takeProductPhotoController = mainStoryboard.instantiateViewControllerWithIdentifier("takeProductPhotoController") as! TakeProductPhotoController rootViewController.pushToViewController(takeProductPhotoController, animated: true) problem is self.window is not available in this VC so getting syntax error – hellosheikh Feb 12 '16 at 20:08
  • okay Thanks for your code its working but the controller is popping up from the bottom like modally controller pops up. I want controller comes up like push segue' – hellosheikh Feb 13 '16 at 05:52
  • secondViewController will be pop from bottom & if you push controller over second controller it will show as push – Rohit Pradhan Feb 13 '16 at 07:21
  • well yes I did that when I write this line self.navigationController?.pushViewController(nav, animated:true) next controller doesn't comes up and if I use this line self.presentViewController(nav, animated:true, completion:nil) the next controller comes up but from the bottom like modally ? – hellosheikh Feb 13 '16 at 07:45
  • You want your ThirdViewController to be pushed.So when you are on SecondVC say like this self.navigationController?.pushViewController(thirdVC, animated:true) – Rohit Pradhan Feb 13 '16 at 07:48
  • okay I tried this. still nothing comes up when I click the button. – hellosheikh Feb 13 '16 at 07:53
  • oh hangon.. I think I am missing something – hellosheikh Feb 13 '16 at 07:55
  • can we discuss this in stackoverflow in chat ? – hellosheikh Feb 13 '16 at 07:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103364/discussion-between-rohit-kp-and-hellosheikh). – Rohit Pradhan Feb 13 '16 at 07:57
1

okay I am posting in detail here what was I doing and what I did to solve my problem in which Rohit Answer helped me.

My FirstVC was connected to SecondVC via segue as presentModally in IB. and then In SecondVC when I tried to push the ThirdVC

let thirdVC = self.storyboard!.instantiateViewControllerWithIdentifier("thirdVC") as! ThirdVC

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

It didn't work.

So to solve this I deleted the segue from IB which was between FirstVC and SecondVC and wrote this code in my firstVC

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

and then in secondVC

let thirdVC =   self.storyboard!.instantiateViewControllerWithIdentifier("thirdVC") as! ThirdVC

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

IT works

hellosheikh
  • 2,929
  • 8
  • 49
  • 115