【问题标题】:set alertview Yes button to bold and No button to normal将 alertview Yes 按钮设置为粗体,将 No 按钮设置为正常
【发布时间】:2014-01-27 11:17:12
【问题描述】:

我有 alertview,我有 Yes 和 No 选项。如下所示。

使用的代码是

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
confAl.tag = 888;
[confAl show];

这很完美,但我希望 Yes 是粗体,No 是普通字体。

所以我切换了是和否按钮,如下所示。

使用的代码是

UIAlertView *confAl = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
confAl.tag = 888;
[confAl show];

有什么方法可以让我们将“是”作为第一个按钮,将“否”作为第二个按钮,并以“是”作为粗体效果?

注意: 我希望在 iOS 6(相同的旧样式)和 iOS 7(如上图的新样式)中也有相同的效果。

【问题讨论】:

  • @downvoter :有什么问题?我真的很喜欢恨你...
  • @Fahim.. 冷静.. 我投了你的票。不要讨厌那个不赞成的选民..:)
  • @JohnWoods :我不想使用自定义警报视图...我正在尝试使用当前警报视图找到解决方案...可能像 firstButton.title.style = bold 或类似的东西...

标签: ios objective-c button uialertview


【解决方案1】:

您可以使用preferredAction 默认属性UIAlertController 而不是UIAlertView

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:noButton];    
[alertController addAction:yesButton];
[alertController setPreferredAction:yesButton];

setPreferredAction 会将您的按钮标题设置为粗体。

【讨论】:

  • 如果我们使用 UIAlertController,这就是答案
  • 由于 UIAlertView 是 deprecated,这是未来的最佳答案
  • 我也在我的代码中使用setPreferredAction。但是这个 crash 在 iOS8 中因为它在 iOS8 中不可用。您有什么解决方案可以在 iOS 8 及更高版本上始终如一地工作吗?
  • 我为上述问题找到了解决方案。我们应该为 API setPreferredAction 添加特定于设备的检查,因为只有在设备是 iOS 9 及更高版本时才应该调用它。此外,我观察到在 iOS 9 以下,UIAlertController 会在内部突出显示最后添加的操作(按钮)。 (Apple doc 中没有提到 UIAlertController 将突出显示最后添加的操作)
【解决方案2】:

您的要求是“突出显示”是按钮。在 iOS 7 中,默认取消按钮是突出显示的按钮。不幸的是,您不能在这里简单地更改 alertViewStyle。但是您确实有解决方法。

this answer

【讨论】:

  • 赞成...你能看看我的问题here
  • @FahimParkar.. 看不到你的截图.. 一个小时后会看到问题.. 如果有任何想法会提示
  • “在 iOS 7 中默认取消按钮是突出显示的”?你确定吗?截图中正好相反。
  • @Mecki.. 是的.. 屏幕截图可能看起来很混乱,这取决于您,您将取消按钮命名为什么。默认情况下,最右边的按钮是取消按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 2013-04-17
  • 2011-02-15
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多