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?
To click a button programmatically with JavaScript, we can call the click method. const userImage = document. getElementById("imageOtherUser"); const hangoutButton = document. getElementById("hangoutButtonId"); userImage.
You can click a button programmatically by using the button. performClick() method.
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.
To fire an event programmatically you need to call sendActionsForControlEvent
button.sendActionsForControlEvents(.TouchUpInside)
--
Swift 3
button.sendActions(for: .touchUpInside)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With