Highlighting an action button

suggest change

Alert controller has a property which is used to put emphases on an action added in the alert controller. This property can be used to highlight a particular action for user attention.For objective C;

@property(nonatomic, strong) UIAlertAction *preferredAction

An action which is already added in alert controller can be assigned to this property.The Alert Controller will highlight this action.

This property can only be used with UIAlertControllerStyleAlert.

Following example shows how to use it.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Cancel edit" message:@"Are you really want to cancel your edit?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    NSLog(@"Cancel");
}];

UIAlertAction *no = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    NSLog(@"Highlighted button is pressed.");
}];

[alertController addAction:cancel];
[alertController addAction:no];

//add no action to preffered action.
//Note
//the action should already be added to alert controller
alertController.preferredAction = no;

[self presentViewController:alertController animated: YES completion: nil];

Alert Controller with preferred action set.The NO button is highlighted.

Alert Controller with preferred action not set.The NO button is not highlighted.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents