Python 如何以编程方式单击按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39336562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to click a button programmatically?
提问by Coding4Life
I have 2 view controllers which should be swapped according to userinput. So, I want to switch the views programatically based on the input I get from a text file.
我有 2 个视图控制器,它们应该根据用户输入进行交换。所以,我想根据我从文本文件中获得的输入以编程方式切换视图。
Algorithm :
if(input == 1)
{
Go to View Controller 1
}
else if(input ==2)
{
Go to View Controller 2
}
Any help on how to click the button programmatically or load that particular viewcontroller with input?
有关如何以编程方式单击按钮或使用输入加载该特定视图控制器的任何帮助?
回答by Adnan Aftab
To fire an event programmatically you need to call sendActionsForControlEvent
要以编程方式触发事件,您需要调用 sendActionsForControlEvent
button.sendActionsForControlEvents(.TouchUpInside)
--
——
Swift 3
斯威夫特 3
button.sendActions(for: .touchUpInside)
回答by Sajjon
Or you can just put all the logic that you perform when a button gets clicked in a separate method, and call that method from your button's selector method.
或者,您可以将单击按钮时执行的所有逻辑放在单独的方法中,然后从按钮的选择器方法中调用该方法。
@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)
}
Then instead of invoking programatically invoking a button press, just call the method pushViewControllerOne()
or pushViewControllerTwo()
然后不是以编程方式调用按钮按下,只需调用方法pushViewControllerOne()
或pushViewControllerTwo()