如何在UIAlertView(iOS)中的其他两个button(堆叠)之间添加Cancelbutton

我想创build一个UIAlertView与三个button(将被堆叠)。 我想要取消button在两个其他button之间的中间。 我已经尝试将cancelButtonIndex设置为1,但是如果有两个其他button,它只是将它们放置在索引0和1.我知道我可以只更改button的名称,但我想要取消button的深蓝色格式。

编辑:**请注意 – 我知道如何得到正确的顺序标题的三个button,但只有当所有三个button本质上看起来像“其他”button; 我想取消button有取消button深蓝色背景 ,使它看起来像一个常规的取消button。 **

我试过了

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease]; alert.cancelButtonIndex = 1; [alert show]; 

 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease]; alert.cancelButtonIndex = 1; [alert addButtonWithTitle:button1Title]; [alert addButtonWithTitle:button2Title]; [alert show]; 

 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease]; alert.cancelButtonIndex = 1; [alert addButtonWithTitle:button2Title]; [alert show]; 

无济于事。 是否有可能完成我想要做的事情?

我有两个辅助点,这个答案。

1)虽然,据我所知,苹果并没有拒绝合理修改UIAlertView的应用程序; 他们曾经说过,像UIAlertView这样的类的视图层次结构应该被认为是私有的。

2)这个问题就是一个很好的例子,为什么你应该提出一个更多关于你的最终目标的问题,而不是到达那里的步骤。 我知道这个问题是关于什么的唯一原因是我在这里的回答留下了一个评论的结果 。

回答:

由于你的评论,我知道你正在寻找创build一个UIAlertView ,即使只有2个button,堆叠button。

我发现这样的代码是最合乎逻辑的地方。 由于通常需要操作alert-view的代码需要在show调用的周围,所以我创build了一个我调用的类别方法,而不是show然后该方法又调用show自身。

 -(void)showWithButtonsStacked{ static NSString *tempButtonTitle = @"SomeUnlikelyToBeUsedTitle"; BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2 if (willAddFakeButton){ self.clipsToBounds = YES; [self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack } BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off [self show]; if (willAddFakeButton){ UIButton *cancelButton = nil; UIButton *tempButton = nil; for (UIButton *button in self.subviews) { if ([button isKindOfClass:[UIButton class]]){ if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){ cancelButton = button; } else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) { tempButton = button; } } } if (hasCancelButton){ // move in cancel button cancelButton.frame = tempButton.frame; } [tempButton removeFromSuperview]; // Find lowest button still visable. CGRect lowestButtonFrame = CGRectZero; for (UIButton *button in self.subviews) { if ([button isKindOfClass:[UIButton class]]){ if (button.frame.origin.y > lowestButtonFrame.origin.y){ lowestButtonFrame = button.frame; } } } // determine new height of the alert view based on the lowest button frame CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5); self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight); } } 

这种方法完成目标的方法是向alert-view添加一个临时button,强制alert-view堆叠button,然后删除临时button并调整高度。 由于它是一种类别方法,您只需调用以下方法即可使用它:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test title" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alert showWithButtonsStacked]; 

此代码导致如下的警报:

在这里输入图像说明

 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease]; [alert addButtonWithTitle:button1Title]; [alert addButtonWithTitle:@"Cancel"]; [alert addButtonWithTitle:button2Title]; [alert show]; 

可能有帮助,

干杯。

 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease]; [alert addButtonWithTitle:button1Title]; [alert addButtonWithTitle:@"Cancel"]; [alert addButtonWithTitle:button2Title]; [alert setCancelButtonIndex:1]; // to make it look like cancel button [alert show]; 

将取消button设置nil ,而不是将其添加到其他button