【问题标题】:UIalertcontroller sequence of buttonsUIalertcontroller 按钮序列
【发布时间】:2016-06-22 18:00:23
【问题描述】:

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

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

这会给我以下结果。我想更改右手边的 Cancel button 和左手边的 OK button

感谢您的宝贵时间。

【问题讨论】:

  • 只有当另一个选项是破坏性操作时,取消按钮才应该在右侧,在这种情况下,您应该使用UIAlertActionStyleDestructive 进行该操作。当右侧为UIAlertActionStyleDefault 时,将其放在左侧是正确的行为

标签: ios uialertcontroller


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 它的作品,谢谢,但我想知道为什么两个警报操作的样式 (UIAlertActionStyleDefault) 相同?我正在尝试 UIAlertActionStyleDefault 和第二个 UIAlertActionStyleCance l!!!
    • 样式取消总是在左边! HIG 并未告诉您取消按钮应始终位于左侧。这取决于用户的便利性。
    • 我同意,还有一点我观察到,当 iOS 9.2 是普通字体取消按钮时,iOs 8.1 会显示粗体取消。但在 ios 9 中,UIAlertActionStyleCance 将始终为粗体。
    • 您始终可以使用preferredAction 方法将uialertAction 加粗。stackoverflow.com/questions/29590534/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多