如何使用swift模拟导航控制器的后退按钮和后退按钮的相同动作



我有两个导航控制器,如下所示:

  1. 导航控制器1->查看控制器1
  2. 选项卡视图控制器->导航控制器2->视图控制器2

当我在View controller2并按下后退按钮时,我想转到View controller1。导航栏中内置的后退按钮可以很好地工作,但当我尝试自己编写时,它不起作用。

我尝试过以下语句,但它们不起作用:

  1. self.dismiss(animated: true, completion: nil)
  2. self.navigationController?.dismiss(animated: true, completion: nil)
  3. navigationController?.popViewController(animated: true)
  4. let story = UIStoryboard(name: "Main", bundle: nil)
let pushVC = story.instantiateViewControllerWithIdentifier("VC1")
let navigation = story.instantiateViewControllerWithIdentifier("Navigation1") as! UINavigationController
navigation.pushViewController(VC1!, animated: true)

如果您只在第一个中使用navigationController,则可以使用它导航到ViewController2:

let vc2 = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
navigationController?.showDetailViewController(vc2, sender: self)

然后,如果您想从viewController2返回到ViewController1,您可以创建取消按钮并连接其操作,并在操作中写入以下内容:

@IBAction func buttonCancelAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}

但是如果在ViewController1&ViewController2,然后您将使用以下内容:

let VC2 = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
navigationController?.pushViewController(VC2, animated: true)

你可以弹出它(解除它(:

@IBAction func buttonCancelAction(_ sender: Any) {
navigationController?.popViewController(animated: true)
}

相关内容

  • 没有找到相关文章

最新更新