UIalertcontroller序列的按钮

Wag*_*agh 2 ios uialertcontroller

我想更改警报操作按钮的顺序,根据代码,我尝试了所有可能性,但不允许我更改序列.

根据HIG,取消在右侧 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Alerts.html#//apple_ref/doc/uid/TP40006556-CH14-SW1

在这里查看我的代码

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];
Run Code Online (Sandbox Code Playgroud)

这会给我以下结果.我想在右手边更改取消,button在左手边更改确定button.

在此输入图像描述

我很感激你的时间.

Tej*_*uri 12

将取消按钮类型的操作样式更改为UIAlertActionStyleDefault而不是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];
Run Code Online (Sandbox Code Playgroud)

现在,您只需更改位置即可更改按钮的顺序

 [alertController addAction:yesButton]; //If you yesButton to appear first (left)
 [alertController addAction:noButton];
Run Code Online (Sandbox Code Playgroud)

UIAlertActions有足够的灵活性来重新排序.它们不是固定的.


Bru*_*lli 12

人们,这是一个有点旧的线程,但如果你想在右键中加上粗体字,你只需要将该操作设置为警告对象的'.preferredAction'属性.希望这可以提供帮助.我刚刚在swift 3/iOS10上证明了它并且它工作得很好.