如何在UIAlertView(iOS)中的其他两个按钮(堆叠)之间添加取消按钮

时间:2012-04-03 15:54:48

标签: iphone ios customization uialertview cancel-button

我正在尝试使用三个按钮(将堆叠)创建UIAlertView。我希望取消按钮位于其他两个按钮之间的中间位置。我已经尝试将cancelButtonIndex设置为1,但是如果有另外两个按钮,它只是将它们放在索引0和1上。我知道我可以只更改按钮的名称,但我想要取消按钮的深蓝色格式

编辑:** 请注意 - 我知道如何以正确的顺序获得带有标题的三个按钮,但前提是所有三个按钮基本上都是“其他”按钮;我希望取消按钮具有取消按钮深蓝色背景,以便它看起来像常规取消按钮。 **

我试过

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

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

4 个答案:

答案 0 :(得分:3)

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

可能会帮助,

干杯。

答案 1 :(得分:2)

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

答案 2 :(得分:2)

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

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

2)这个问题就是一个很好的例子,说明为什么你应该提出更多关于你的最终目标的问题,而不是去实现目标的步骤。我知道这个问题的唯一原因是我的回答留下了评论here

<强>答案:

由于您的评论,我知道即使只有2个按钮,您也希望创建一个堆叠按钮的UIAlertView

我发现像这样的代码最合乎逻辑的地方属于一个类别。由于操作警报视图所需的代码通常需要围绕show调用,因此我创建了一个我调用的类别方法而不是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);        
    }
}

此方法实现目标的方法是向警报视图添加临时按钮以强制警报视图堆叠按钮,然后删除临时按钮并调整高度。由于它是一种类别方法,因此只需调用:

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

此代码会产生如下警告:

enter image description here

答案 3 :(得分:0)

将取消按钮设置为nil,然后将其添加到其他按钮