UIalertcontroller 按钮序列

UIalertcontroller sequence of buttons

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

根据 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

感谢您的宝贵时间。

将取消按钮类型的操作样式更改为 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 足够灵活,可以重新排序。它们不是固定的。

朋友们,这是一个有点老的线程,但是如果你想在右键中使用粗体字体,你只需要将警报对象的操作设置为“.preferredAction”属性。希望这能有所帮助。我刚刚在 swift 3 / iOS10 上证明了它,它工作得很好。