Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click a button programmatically?

Tags:

python

swift

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.

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?

like image 410
Coding4Life Avatar asked Sep 05 '16 19:09

Coding4Life


People also ask

Can JavaScript click a button programmatically?

To click a button programmatically with JavaScript, we can call the click method. const userImage = document. getElementById("imageOtherUser"); const hangoutButton = document. getElementById("hangoutButtonId"); userImage.

How can I click programmatically in Android?

You can click a button programmatically by using the button. performClick() method.

How do you click on JavaScript?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.


2 Answers

To fire an event programmatically you need to call sendActionsForControlEvent

button.sendActionsForControlEvents(.TouchUpInside) 

--

Swift 3

button.sendActions(for: .touchUpInside) 
like image 101
Adnan Aftab Avatar answered Sep 29 '22 01:09

Adnan Aftab


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()

like image 25
Sajjon Avatar answered Sep 29 '22 01:09

Sajjon