UIalertcontroller序列的按钮

时间:2016-06-22 18:00:23

标签: 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];

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

enter image description here

我很感激你的时间。

2 个答案:

答案 0 :(得分:10)

将取消按钮类型的操作样式更改为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];

现在您只需更改

的位置即可更改按钮的顺序
 [alertController addAction:yesButton]; //If you yesButton to appear first (left)
 [alertController addAction:noButton];

UIAlertActions足够灵活,可以重新排序。它们没有修复。

答案 1 :(得分:9)

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