Cancel Button in UIAlertController with UIAlertControllerStyle.ActionSheet

36.2k views Asked by At

I want to add a separated cancel button to my UIAlert.

I know how to do it with UIActionSheet but it should also be possible with UIAlert, right?

var sheet: UIActionSheet = UIActionSheet();
    let title: String = "...";
    sheet.title  = title;
    sheet.delegate = self;
    sheet.addButtonWithTitle("Cancel");
    sheet.addButtonWithTitle("...")
    sheet.cancelButtonIndex = 0;
    sheet.showInView(self.view);

This will have a ... button and a cancel button which is separated.

So does anyone know how to do this with

    var alert = UIAlertController(title: "...", message: "....", preferredStyle: UIAlertControllerStyle.ActionSheet)

?

I'm new to xcode and swift so sorry if this question is dumb or anything...

3

There are 3 answers

4
Jack Chorley On BEST ANSWER

Its really simple, but works a bit differently to how they used to work. Now you add "actions" to your alerts. These actions are then represented by buttons on the device.

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

Above is the code needed for a simple cancel button - bear in mind that dismissal of the alert is done automatically so don't put that in your handler. Should you then want to create another button which does something, use the code below:

alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: { action in
        println("This button now calls anything inside here!")
    }))

Hopefully I have understood your question and this answers what you were asking. I will also add that after you have added all of the "actions", you present the alert using the code below:

self.presentViewController(alert, animated: true, completion: nil)

Hope this helps!

1
Pradeep Mittal On

This might be the worst coded answer you would have seen, but I was able to meet your requirement by trying this:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UILabel *alertLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertController.view.frame.size.width, 2)];
alertLine.backgroundColor=[UIColor blackColor];
[alertController.view.preferredFocusedView addSubview:alertLine];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self.navigationController presentViewController:alertController animated:YES completion:nil];
1
AudioBubble On

I wanted to go ahead and provide a specific answer for a specific question. The user asked about the implementation of a "cancel" button, not a default button. Check out the answer below!

let alertController = UIAlertController(title: "Select one", message: "Hey! Press a button", preferredStyle: .actionSheet)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

alertController.addAction(cancelAction)

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