如何在UIAlertView(iOS)中的其他两个按钮(堆叠)之间添加“取消"按钮 [英] How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)
问题描述
我试图用三个按钮(将被堆叠)创建一个UIAlertView.我希望取消"按钮位于其他两个按钮之间.我已经尝试将cancelButtonIndex设置为1,但是如果还有其他两个按钮,则将它们简单地放置在索引0和1上.我知道我可以更改按钮的名称,但是我想取消按钮的颜色为深蓝色
**请注意-我知道如何以正确的顺序获得带有标题的三个按钮,但前提是所有三个按钮本质上看起来都像其他"按钮;我希望取消按钮具有取消按钮深蓝色背景,以便它看起来像常规的取消按钮.**
我尝试过
UIAlertView * alert = [[[[UIAlertView alloc] initWithTitle:title消息:msg委托:self cancelButtonTitle:@取消" otherButtonTitles:button1Title,button2Title,nil]自动释放];alert.cancelButtonIndex = 1;[警报显示];
和
UIAlertView * alert = [[[[UIAlertView alloc] initWithTitle:title消息:msg委托:self cancelButtonTitle:@取消" otherButtonTitles:nil]自动释放];alert.cancelButtonIndex = 1;[alert addButtonWithTitle:button1Title];[alert addButtonWithTitle:button2Title];[警报显示];
和
UIAlertView * alert = [[[[UIAlertView alloc] initWithTitle:title消息:msg委托:self cancelButtonTitle:@取消" otherButtonTitles:addButtonWithTitle:button1Title,nil]自动释放];alert.cancelButtonIndex = 1;[alert addButtonWithTitle:button2Title];[警报显示];
无济于事.甚至有可能完成我想做的事情?
此答案有两个辅助点.
1)据我所知,Apple并未拒绝对 UIAlertView
进行合理修改的应用程序;他们说过,像 UIAlertView
这样的类的视图层次结构应该被视为私有的.
2)这个问题很好地说明了为什么您应该问更多关于最终目标的问题,而不是要达到目标的步骤.我知道这个问题的唯一原因是此处.>
答案:
由于您的评论,我知道您希望创建一个 UIAlertView
,即使只有2个按钮,它也具有堆叠的按钮.
我发现这样的代码最合逻辑的位置是在类别中.由于通常操纵警报视图所需的代码需要围绕 show
调用,因此我创建了我调用的类别方法,而不是 show
,该方法又调用了显示
本身.
-(void)showWithButtonsStacked {静态NSString * tempButtonTitle = @"SomeUnlikelyToBeUsedTitle";BOOL willAddFakeButton =(self.numberOfButtons == 2);//只有2个按钮并排如果(willAddFakeButton){self.clipsToBounds = YES;[self addButtonWithTitle:tempButtonTitle];//添加临时按钮,以便Alertview可以堆叠}布尔hasCancelButton =(self.cancelButtonIndex!= -1);//如果有一个取消按钮,我们不想切断它[自我展示];如果(willAddFakeButton){UIButton * cancelButton = nil;UIButton * tempButton = nil;对于(UIButton * self.subviews中的按钮){如果([button isKindOfClass:[UIButton类]]){如果(hasCancelButton&& [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){cancelButton =按钮;}否则,如果([button.titleLabel.text isEqualToString:tempButtonTitle]){tempButton =按钮;}}}if(hasCancelButton){//移动取消按钮cancelButton.frame = tempButton.frame;}[tempButton removeFromSuperview];//查找仍然可见的最低按钮.CGRect minimumButtonFrame = CGRectZero;对于(UIButton * self.subviews中的按钮){如果([button isKindOfClass:[UIButton类]]){如果(button.frame.origin.y> lowestButtonFrame.origin.y){minimumButtonFrame = 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:@测试标题"消息:@消息"委托:自我cancelButtonTitle:@取消" otherButtonTitles:@"OK",无];[alert showWithButtonsStacked];
此代码会产生如下警报:
I am trying to create a UIAlertView with three buttons (which will be stacked). 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.
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?
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
; They have said that the view hierarchy of classes like UIAlertView
should be considered private.
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. 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.
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.
-(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(iOS)中的其他两个按钮(堆叠)之间添加“取消"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!