2

I would like to change the sequence of alert action button, as per code I tried all possibility but it is not allowed me to change the sequence.

As per HIG, cancel is on right hand side https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Alerts.html#//apple_ref/doc/uid/TP40006556-CH14-SW1

See my code here

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* noButton = [UIAlertAction
                               actionWithTitle:@"Cancel"
                               style:UIAlertActionStyleCancel
                               handler:^(UIAlertAction * action)
                               {
                                   [alertController dismissViewControllerAnimated:YES completion:nil];
                               }];

UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"OK"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {

                                    [alertController dismissViewControllerAnimated:YES completion:nil];

                                }];


[alertController addAction:yesButton];
[alertController addAction:noButton];
[self presentViewController:alertController animated:YES completion:nil];

this will give me following result. I want to change Cancel button on right hand and OK button on left hand side.

enter image description here

I appreciate for your time.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
Wagh
  • 2,678
  • 1
  • 25
  • 29
  • The cancel button should only be on the right if the other option is a destructive action, in which case you should use `UIAlertActionStyleDestructive` for that action. Having it on the left is the correct behavior when the right side is `UIAlertActionStyleDefault` – dan Jun 22 '16 at 18:29

2 Answers2

18

People, it's a little old thread but if you want to have the bold font in the right button, you just need to set that action as '.preferredAction' property for the alert object. Hope this can help. I've just proved it on swift 3 / iOS10 and it works just fine.

WINSergey
  • 1,977
  • 27
  • 39
Bruno Garelli
  • 235
  • 2
  • 7
13

Change the action style for cancel button type to UIAlertActionStyleDefault instead of UIAlertActionStyleCancel

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"My alert message" preferredStyle:UIAlertControllerStyleAlert];


UIAlertAction* yesButton = [UIAlertAction
                            actionWithTitle:@"OK"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {

                                [alertController dismissViewControllerAnimated:YES completion:nil];

                            }];

UIAlertAction* noButton = [UIAlertAction
                           actionWithTitle:@"Cancel"
                           style:UIAlertActionStyleDefault
                           handler:^(UIAlertAction * action)
                           {
                               [alertController dismissViewControllerAnimated:YES completion:nil];
                           }];

[alertController addAction:yesButton];
[alertController addAction:noButton];


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

And now you can change the order of the buttons by simply changing the positions of

 [alertController addAction:yesButton]; //If you yesButton to appear first (left)
 [alertController addAction:noButton];

The UIAlertActions are flexible enough to reorder. They are not fixed .

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • 1
    its works, thanks but I was wondering why style are (UIAlertActionStyleDefault) same for both alert actions? I was trying UIAlertActionStyleDefault and second one UIAlertActionStyleCance l!!! – Wagh Jun 22 '16 at 18:17
  • The style Cancel will alwyas be in the left side! The HIG doesn't tell you that cancel button should always be on left. It is upto user convinience. – Teja Nandamuri Jun 22 '16 at 18:21
  • I agreed, one more point I observe, iOs 8.1 will display bold cancel when iOS 9.2 is normal font cancel button. But in ios 9, UIAlertActionStyleCance will always be in bold letter. – Wagh Jun 22 '16 at 18:26
  • you can always bold the uialertAction using the preferredAction method.http://stackoverflow.com/questions/29590534/ios-uialertcontroller-bold-button-changed-in-8-3 – Teja Nandamuri Jun 22 '16 at 18:29