Action Sheets with UIAlertController

suggest change

With UIAlertController, action sheets like the deprecated UIActionSheet are created with the same API as you use for AlertViews.

Simple Action Sheet with two buttons

Swift

let alertController = UIAlertController(title: "Demo", message: "A demo with two buttons", preferredStyle: UIAlertControllerStyle.actionSheet)

Objective-C

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Demo" message:@"A demo with two buttons" preferredStyle:UIAlertControllerStyleActionSheet];

Create the buttons “Cancel” and “Okay”

Swift

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
}

Objective-C

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:

Swift

alertController.addAction(cancelAction)
alertController.addAction(okAction)

Objective-C

[alertController addAction:cancelAction];
[alertController addAction:okAction];

Now present the UIAlertController:

Swift

self.present(alertController, animated: true, completion: nil)

Objective-C

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

This should be the result:

Action Sheet with destructive button

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:

Swift

let destructiveAction = UIAlertAction(title: "Delete", style: .destructive) { (result : UIAlertAction) -> Void in
    //action when pressed button
}

Objective-C

UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
            //action when pressed button
        }];

Feedback about page:

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


UIAlertController:
* Action Sheets with UIAlertController

Table Of Contents
12 UIView
13 UIAlertController
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable