更改UIAlertView中取消按钮的位置?

arl*_*dia 12 iphone hig uialertview ios4 ios

我注意到当我从iPhone主屏幕删除某个应用程序时,出现的警报视图会在左侧显示"删除"按钮,在右侧显示"取消".但是,当我使用UIAlertView在我的应用程序中构建删除功能时,按钮似乎只显示左侧的取消和右侧的删除.

我希望我的应用程序与操作系统保持一致,但我无法弄清楚如何首先显示"取消"按钮.有人知道吗?

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Delete Song" 
                      message:@"Are you sure you want to delete this song? This will permanently remove it from your database." 
                      delegate:self 
                      cancelButtonTitle:@"Cancel" 
                      otherButtonTitles:@"Delete", nil];
Run Code Online (Sandbox Code Playgroud)

我尝试设置alert.cancelButtonIndex = 1,但这没有任何效果.

arl*_*dia 52

啊,我刚想出如何改变这一点.cancelButtonTitle参数是可选的,因此您可以在任何您想要的位置添加自定义按钮,然后将其指定为取消按钮,如下所示:

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Delete Song" 
                      message:@"Are you sure you want to delete this song? This will permanently remove it from your database." 
                      delegate:self 
                      cancelButtonTitle:nil
                      otherButtonTitles:@"Delete", @"Cancel", nil];
alert.cancelButtonIndex = 1;
Run Code Online (Sandbox Code Playgroud)

这会将"删除"按钮放在左侧,将"取消"按钮放在右侧,并突出显示"取消"按钮.

  • 我发现这不再适用于iOS 8.3(在模拟器中),但在iOS 7.1上工作正常.尽管事实上一些苹果警报仍然*做*右边的"取消",即使在8.3.(我想知道他们是怎么做到的!) (4认同)
  • +1很好,绝对是苹果打算超出我的建议. (3认同)

Bol*_*ock 4

苹果在主屏幕上使用警报视图的一个可能原因是它曾经要求用户对他们要删除的应用程序进行评分(不再是了)。他们可能将“取消”按钮设为浅色,因为这被认为是一种破坏性操作(删除应用程序及其数据)。

我想你可以颠倒标题 ( cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil) 并以相反的方式处理这些按钮的点击(不确定 Apple 是否也这样做)。但这会有点尴尬;使用操作表怎么样?

  • [http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html%23//apple_ref/doc/uid/TP40006556-CH13-SW41](HIG)指定:_“在提出潜在危险操作的两按钮警报中,取消操作的按钮应位于右侧且为浅色。在提出人们可能想要的良性操作的两按钮警报中,取消操作的按钮应该位于左侧,并且是深色的“_。我想这就是按钮的顺序/颜色不同的原因。 (2认同)