iOS User Alerts and Action Sheets
In Cocoa Touch, you can send alert to the user using the UIAlertView
and UIActionSheet
classes.
UIAlertView
UIAlertView
allows you to send messages to the user. The class UIAlertView
is used together to the UIAlertViewDelegate
protocol. In this way, when the user tap on an alert view button, we can react to that, using the following method:
1 |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex (NSInteger)buttonIndex; |
Or in Swift:
1 |
optional func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) |
To launch an alert is very simple, you instantiate an alert using the following method:
1 |
-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,... |
Or in Swift:
1 |
init(title title: String?, message message: String?, delegate delegate: AnyObject?, cancelButtonTitle cancelButtonTitle: String?) |
Here, an example on how to use this method:
1 2 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Alert” message:@”This is an alert” delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:nil]; [alert show]; |
1 2 3 4 5 |
let alert = UIAlertView(title: "Alert", message: "This is an alert", delegate: self, cancelButtonTitle: nil) alert.addButtonWithTitle("one") alert.addButtonWithTitle("two") alert.delegate = self alert.show() |
When the user taps on the Cancel button, the delegate method alertView:clickedButtonAtIndex
is executed on the alert view delegate object. The button index depends on the button position. The first button on the left has index 0.
UIActionSheet
UIActionSheet
works in a similar way of the UIAlertView
. An action sheet is needed to ask the user to take a decision. Action sheets can be visualized in a view, from a tab bar or from a navigation bar.
If your class uses an action sheet, it must conform to the UIActionSheetDelegate
protocol. In this way, you can also implement the following method from the UIActionSheetDelegate
protocol:
1 |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex; |
1 |
optional func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) |
This method allows us to process the user action. To launch an action is very simple too. Whenever you need an action sheet, you initialize one using the following method:
1 |
- (id)initWithTitle:(NSString *)title delegate:()delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, … |
1 |
init(title title: String?, delegate delegate: UIActionSheetDelegate?, cancelButtonTitle cancelButtonTitle: String?, destructiveButtonTitle destructiveButtonTitle: String?) |
Here an example on how to use the previous method:
1 2 |
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Action" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK" otherButtonTitles:nil]; [action showInView:self.view]; |
1 2 3 4 5 |
let action = UIActionSheet(title: "Action", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "OK") action.addButtonWithTitle("one") action.addButtonWithTitle("two") action.delegate = self action.showInView(self.view) |
A practical example
Let’s build an iOS application using an alert view. Open Xcode and create a Single-View Application. Call it SimpleAlert. Open the ViewController and add the UIAlertViewDelegate
protocol.
1 2 |
@interface ViewController () @end |
1 |
class ViewController: UIViewController, UIAlertViewDelegate { } |
Add the following method:
1 |
- (IBAction)alertViewButtonPressed:(id)sender { } |
1 |
@IBAction func alertViewButtonPressed(sender: UIButton) { } |
Now, go to Main.storyboard, add a UIButton
and connect to the alertViewButtonPressed
IBAction. Back in ViewController.m, complete the alertViewButtonPressed
method:
1 2 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an alert" delegate:self cancelButtonTitle:nil otherButtonTitles:@"one", @"two", @"three", nil]; [alert show]; |
1 2 3 4 5 |
let alert = UIAlertView(title: "Alert", message: "This is an alert", delegate: self, cancelButtonTitle:nil alert.addButtonWithTitle("one") alert.addButtonWithTitle("two") alert.addButtonWithTitle("three") alert.show() |
Then, implement the following delegate method to notify the delegate when the user clicks a button on an alert view:
1 2 3 |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex (NSInteger)buttonIndex { NSLog(@”You pressed button %d”,buttonIndex+1); } |
1 2 3 |
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { NSLog("You pressed button %ld", buttonIndex+1) } |
Build and run and now, when you press one of the buttons, a message should appear in the console with the button index you pressed.
Have a nice coding,
Geppy