With UIAlertController
, action sheets like the deprecated UIActionSheet
are created with the same API as you use for AlertViews.
let alertController = UIAlertController(title: "Demo", message: "A demo with two buttons", preferredStyle: UIAlertControllerStyle.actionSheet)
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Demo" message:@"A demo with two buttons" preferredStyle:UIAlertControllerStyleActionSheet];
Create the buttons "Cancel" and "Okay"
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (result : UIAlertAction) -> Void in
//action when pressed button
}
let okAction = UIAlertAction(title: "Okay", style: .default) { (result : UIAlertAction) -> Void in
//action when pressed button
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
//action when pressed button
}];
UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//action when pressed button
}];
And add them to the action sheet:
alertController.addAction(cancelAction)
alertController.addAction(okAction)
[alertController addAction:cancelAction];
[alertController addAction:okAction];
Now present the UIAlertController
:
self.present(alertController, animated: true, completion: nil)
[self presentViewController:alertController animated: YES completion: nil];
This should be the result:
Using the UIAlertActionStyle
.destructive
for an UIAlertAction
will create a button with red tint color.
For this example, the okAction
from above was replaced by this UIAlertAction
:
let destructiveAction = UIAlertAction(title: "Delete", style: .destructive) { (result : UIAlertAction) -> Void in
//action when pressed button
}
UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
//action when pressed button
}];