我有两个viewCcontroller,FirstViewController和SecondViewController。现在我在FirstViewController中显示一个popoverpresentationcontroller,它有一个按钮,有几个textFields,用户可以输入。现在,一旦单击按钮,我想将其导航到SecondViewController并传递数据。但是我无法导航到第二个视图控制器。如果我使用来自popoverpresentationcontroller按钮的segue到SecondViewController它成功导航,但我想以编程方式实现。有关如何做到这一点的任何帮助??
这是我的按钮点击代码
@IBAction func goTo(_ sender: UIButton) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let destination = storyBoard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
destination.flag = flag
sender: sender)
self.navigationController?.pushViewController(destination, animated: true)
}
}
}
答案 0 :(得分:0)
由于呈现的视图控制器上没有navigation controller
,您无法导航
首先,您必须添加navigation controller
,然后才能执行导航。
let nav1 = UINavigationController()
nav1.viewControllers = [self]
nav1.pushViewController(child, animated: true)
答案 1 :(得分:0)
我只是稍微清理你的代码。
@IBAction func goTo(_ sender: UIButton) {
let destinationViewController = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
destinationViewController.flag = flag
self.navigationController?.pushViewController(destinationViewController, animated: true)
}
请注意在Main.storyboard中设置viewController