我有2个视图控制器,应根据用户输入进行交换。所以,我想根据我从文本文件中获得的输入以编程方式切换视图。
Algorithm :
if(input == 1)
{
Go to View Controller 1
}
else if(input ==2)
{
Go to View Controller 2
}
有关如何以编程方式单击按钮或使用输入加载特定viewcontroller的任何帮助吗?
答案 0 :(得分:77)
要以编程方式触发事件,您需要致电sendActionsForControlEvent
button.sendActionsForControlEvents(.TouchUpInside)
-
Swift 3
button.sendActions(for: .touchUpInside)
答案 1 :(得分:3)
或者您可以在单独的方法中单击按钮时放置您执行的所有逻辑,并从按钮的选择器方法中调用该方法。
@IBAction func someButtonPressed(button: UIButton) {
pushViewControllerOne()
}
@IBAction func someButtonPressed(button: UIButton) {
pushViewControllerTwo()
}
func pushViewControllerOne() {
let viewController = ViewControllerOne(nibName: "ViewControllerOne", bundle: nil)
pushViewController(viewController)
}
func pushViewControllerTwo() {
let viewController = ViewControllerOne(nibName: "ViewControllerTwo", bundle: nil)
pushViewController(viewController)
}
func pushViewController(viewController: UIViewController) {
navigationController?.pushViewController(viewController, animated: true)
}
然后调用方法pushViewControllerOne()
或pushViewControllerTwo()