简体   繁体   English

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

[英]How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)

I am trying to create a UIAlertView with three buttons (which will be stacked). 我试图用三个按钮(将被堆叠)创建一个UIAlertView。 I would like the Cancel button to be in the middle, between the two other buttons. 我希望“取消”按钮位于其他两个按钮之间。 I have tried setting the cancelButtonIndex to 1, but if there are two other buttons, it simply places them at indexes 0 and 1. I know I could just change the names of the buttons, but I want the darker blue formatting of the cancel button. 我曾尝试将cancelButtonIndex设置为1,但是如果还有其他两个按钮,它只会将它们放置在索引0和1上。我知道我可以更改按钮的名称,但是我想取消按钮的颜色为深蓝色。

EDIT: ** Please note - I know how to get the three buttons with the titles in the correct order, but only if all three buttons essentially look like 'other' buttons; 编辑:**请注意-我知道如何以正确的顺序获取带有标题的三个按钮,但前提是所有三个按钮本质上看起来都像“其他”按钮; I want the cancel button to have the cancel button dark blue background so that it will look like a regular cancel button. 我希望取消按钮具有深蓝色背景的取消按钮,以便看起来像常规的取消按钮。 ** **

I've tried 我试过了

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

and

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

and

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

to no avail. 无济于事。 Is it even possible to accomplish what I am trying to do? 甚至有可能实现我想做的事情?

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

Might Help, 可能有帮助,

Cheers. 干杯。

I have two ancillary points to this answer. 对于这个答案,我有两个辅助要点。

1) While, to the best of my knowledge, Apple has not rejected an app for reasonable modification of a UIAlertView ; 1)据我所知,Apple并未拒绝对UIAlertView进行合理修改的应用程序; They have said that the view hierarchy of classes like UIAlertView should be considered private. 他们说过,像UIAlertView这样的类的视图层次结构应被视为私有的。

2) This question is a good example of why you should ask a question more about your end goal rather than the steps to get there. 2)这个问题很好地说明了为什么您应该问更多关于最终目标的问题,而不是要达到目标的步骤。 The only reason I know what this question is about is as a result of a comment left at my answer here . 我知道这个问题的唯一原因是我在这里的答案中留下了评论。

Answer: 回答:

Because of your comment I know that you are looking to create a UIAlertView that has stacked buttons even when there are only 2 buttons. 由于您的评论,我知道您希望创建一个UIAlertView ,该UIAlertView具有堆叠的按钮,即使只有2个按钮也是如此。

I find the most logical place for code like this is in a category. 我发现这样的代码最合理的位置是在类别中。 Since generally the code needed to manipulate the alert-view needs to be around the show call, I created a category method I call instead of show and the method in turn calls show itself. 由于通常需要对警报视图进行操作的代码必须围绕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);        
    }
}

The way this method accomplishes it's goal is to add a temporary button to the alert-view to force the alert-view to stack the buttons, then it removes the temporary button and adjusts the height. 此方法完成其目标的方式是向警报视图添加一个临时按钮,以强制警报视图堆叠按钮,然后删除该临时按钮并调整高度。 Since it's a category method you use it simply by calling: 由于它是一个类别方法,因此只需调用以下命令即可使用它:

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

This code results in an alert like this: 此代码会产生如下警告:

在此输入图像描述

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM