2

我正在尝试使用三个按钮(将被堆叠)创建一个 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

4 回答 4

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

可能有帮助,

干杯。

于 2012-04-03T16:03:47.860 回答
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];
于 2012-04-03T16:30:13.017 回答
2

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

1) 虽然,据我所知,Apple 并没有因为对 a 的合理修改而拒绝应用UIAlertView;他们说类的视图层次结构UIAlertView应该被认为是私有的。

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

回答:

由于您的评论,我知道您正在寻求创建一个UIAlertView具有堆叠按钮的按钮,即使只有 2 个按钮也是如此。

我发现像这样的代码最合乎逻辑的地方是在一个类别中。由于通常操作警报视图所需的代码需要围绕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];

此代码会产生如下警报:

在此处输入图像描述

于 2012-04-03T20:30:49.583 回答
0

将取消按钮设置为nil并将其添加到其他按钮中

于 2012-04-03T16:04:30.087 回答