Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call a Function when Ok is pressed in an UIAlert

Im try to simply call a function called "GoToNewShoot" When the user press the "ok" button when the Alert pops up Thanks!.

Code:

   @IBAction func GobacktoCamera(sender: AnyObject) {
    var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over ", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
like image 710
Hunter Avatar asked Sep 12 '15 05:09

Hunter


People also ask

Can we call function in alert?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

What is the function alert () used for?

The alert() method displays an alert box with a message and an OK button. The alert() method is used when you want information to come through to the user.

How do you alert a function in JavaScript?

The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.

Can I put HTML in an alert?

You can add HTML into an alert string, but it will not render as HTML.


1 Answers

You can do it by using handler from addAction this way:

@IBAction func GobacktoCamera(sender: AnyObject) {

    var alertController = UIAlertController(title: "Just Checking", message: "Are You Sure You Want to Start over", preferredStyle: UIAlertControllerStyle.Alert)

    alertController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { action in
        //run your function here
        self.GoToNewShoot()
    }))

    self.presentViewController(alertController, animated: true, completion: nil)
}


func GoToNewShoot(){

    println("Method Called")
}
like image 86
Dharmesh Kheni Avatar answered Oct 27 '22 01:10

Dharmesh Kheni