2011-05-13 279 views
1

我需要在iphone项目的alert view的2个默认按钮下插入一个视图(一个自定义按钮)。 我搜索了很久,但我没有找到如何在按钮下插入元素,提示?将元素添加到UIAlertview

+0

除非您想使用'otherButtonTitles'添加按钮,否则如果您需要更好的东西,您可能需要考虑推出自己的按钮。同样,它可能很难说是否可以接受AppStore。 – 2011-05-13 07:57:54

+0

看看这个链接可能有帮助 http://stackoverflow.com/questions/5576031/iphone-alertview-with-textfield – 2011-05-13 07:55:47

回答

0

不完全清楚你想要做什么,但有几种可能性。如果你只是想要常规的按钮,你可以使用其他按钮参数来粘贴尽可能多的按钮。

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Other", @"Other2", nil]; 

您还可以使用addButtonWithTitle添加任意多的。

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"OK", nil]; 
[anAlert addButtonWithTitle:@"My Other Button"]; 

如果你真的想要一个自定义按钮,事情会变得更加棘手。干扰任意观看UIAlertViews是我的一个爱好(请参阅我的博客文章主题hereherehere)。但是,在所有这些情况下,观看结果都在正常按钮之上。按钮固定在视图的底部,我知道无法改变这种行为。

你可以做的一件事是创建没有按钮的UIAlertView,然后创建并添加尽可能多的自定义按钮。首先,我想指出,这可能是一个坏主意。这不会是一个很好的用户体验,除非你是TapBots,坚持Apple's HIG通常是一个好主意。

说了这么多,这样的事情可能工作:

UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 

UIButton* okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
okButton.frame = CGRectMake(12, 80, 130, 50); 
[okButton setTitle:@"OK" forState:UIControlStateNormal]; 
[anAlert addSubview:okButton]; 

UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
cancelButton.frame = CGRectMake(144, 80, 130, 50); 
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; 
[anAlert addSubview:cancelButton]; 

UIButton* otherButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
otherButton.frame = CGRectMake(12, 136, 260, 50); 
[otherButton setTitle:@"My Other Button" forState:UIControlStateNormal]; 
[anAlert addSubview:otherButton]; 

在警报消息末尾的换行符水平扩展的警报视图,使居室增添你的按钮。在这里,我只是在做简单的UIButtons(看起来很可怕);希望你的自定义按钮看起来更好。您需要通过对坐标进行硬编码(这些坐标很脆弱(并根据您的消息和屏幕方向进行更改)来自行布局)。最后,由于没有任何默认按钮,因此无法解除警报,所以您必须确保在这些按钮上粘贴处理程序才能实现此目的。您可以通过按钮上的addTarget:action:forControlEvents:进行此操作。