How to add action to UIAlertView in Swift (iOS 7)

I want to add an action to the Retry button, but when the user clicks the Retry button, nothing happens.

var createAccountErrorAlert: UIAlertView = UIAlertView() createAccountErrorAlert.delegate = self createAccountErrorAlert.title = "Oops" createAccountErrorAlert.message = "Could not create account!" createAccountErrorAlert.addButtonWithTitle("Dismiss") createAccountErrorAlert.addButtonWithTitle("Retry") createAccountErrorAlert.show() 

Function for determining the index of the pressed button?

 func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ switch buttonIndex{ case 1: createAccount() default: //Some code here.. } } 
+7
ios swift
source share
4 answers

I tested your code and it works great for me, it prints the corresponding result:

 func showAlert(){ var createAccountErrorAlert: UIAlertView = UIAlertView() createAccountErrorAlert.delegate = self createAccountErrorAlert.title = "Oops" createAccountErrorAlert.message = "Could not create account!" createAccountErrorAlert.addButtonWithTitle("Dismiss") createAccountErrorAlert.addButtonWithTitle("Retry") createAccountErrorAlert.show() } func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ switch buttonIndex{ case 1: NSLog("Retry"); break; case 0: NSLog("Dismiss"); break; default: NSLog("Default"); break; //Some code here.. } } 

Turn off printing when I click cancel.

+22
source share

This is because the index of your redo button is 1, not 0. The fire button has an index of 0.

If you place the following code on the playground, you will see the button indices (as described in the addButtonWithTitle method):

 var createAccountErrorAlert: UIAlertView = UIAlertView() createAccountErrorAlert.title = "Oops" createAccountErrorAlert.message = "Could not create account!" createAccountErrorAlert.addButtonWithTitle("Dismiss") //Prints 0 createAccountErrorAlert.addButtonWithTitle("Retry") //Prints 1 
0
source share

The pointer is invalid. For me (based on the default application template for one default view for iOS) it works (where I connected the button to launch @IBAction buttonPressed ). Remember to make your controller look like UIAlertViewDelegate (although without this, follow these steps):

 import UIKit class ViewController: UIViewController, UIAlertViewDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func buttonPressed(sender : AnyObject) { var createAccountErrorAlert: UIAlertView = UIAlertView() createAccountErrorAlert.delegate = self createAccountErrorAlert.title = "Oops" createAccountErrorAlert.message = "Could not create account!" createAccountErrorAlert.addButtonWithTitle("Dismiss") createAccountErrorAlert.addButtonWithTitle("Retry") createAccountErrorAlert.show() } func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { switch buttonIndex { default: println("alertView \(buttonIndex) clicked") } } } 
0
source share

This is how I did it with shorter lines of code:

 func showAlert(){ var alert = UIAlertView(title: "Choose", message: "Which_pill", delegate: self, cancelButtonTitle:"Red") alert.addButtonWithTitle("Blue") alert.show() } func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){ switch buttonIndex{ case 1: println("Blue") case 0: println("Red") default: println("Is this part even possible?") } } 
0
source share

All Articles