push is not working on the controller which was presented modally

时间:2016-02-12 19:25:57

标签: ios swift segue uistoryboardsegue

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

2 个答案:

答案 0 :(得分: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)}

答案 1 :(得分:1)

好的我在这里详细介绍了我在做什么以及我为解决Rohit Answer帮助我的问题所做的工作。

我的FirstVC已在IB中通过segue与SecondVC连接到presentModally。当我试图推动ThirdVC

时,在 SecondVC
let thirdVC = self.storyboard!.instantiateViewControllerWithIdentifier("thirdVC") as! ThirdVC

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

它不起作用。

为了解决这个问题,我删除了IB中的segue,它位于 FirstVC SecondVC 之间,并在我的 firstVC

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

然后在 secondVC

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

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

IT工作