Objective c 向UIAlertview添加元素

Objective c 向UIAlertview添加元素,objective-c,uialertview,Objective C,Uialertview,我需要在iphone项目中警报视图的两个默认按钮下插入一个视图(自定义按钮)。 我搜索了很长一段时间,但没有找到如何在按钮、提示下插入元素?不太清楚您想做什么,但有一些可能性。如果您只需要常规按钮,可以使用otherButtons参数将任意数量的按钮粘贴在上面 UIAlertView* anAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"This is My Alert" delegate:self cancelB

我需要在iphone项目中警报视图的两个默认按钮下插入一个视图(自定义按钮)。
我搜索了很长一段时间,但没有找到如何在按钮、提示下插入元素?

不太清楚您想做什么,但有一些可能性。如果您只需要常规按钮,可以使用otherButtons参数将任意数量的按钮粘贴在上面

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是我的一种爱好(请参阅我关于该主题的博客文章,和)。然而,在所有这些情况下,视图最终都位于正常按钮之上;按钮固定在视图的底部,我知道没有办法改变这种行为

您可以做的一件事是创建没有按钮的UIAlertView,然后创建并添加任意数量的自定义按钮。首先,我想指出,这可能是个坏主意。这将不是一个好的用户体验,除非你是,坚持通常是一个好主意

话虽如此,类似的方法可能会奏效:

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(看起来很可怕);希望你的定制按钮看起来更好。您需要自己通过硬编码坐标来布置它们,这很脆弱(并且根据您的消息和屏幕方向而变化)。最后,由于没有任何默认按钮,因此无法消除警报,因此您必须确保在这些按钮上粘贴处理程序以实现此目的。你可以用<代码> Advest:Actuff:FordPrimes:按钮上的来做。

查看这个链接可能会有帮助,除非你想用“代码>其他按钮标题< /Cord>添加按钮,如果你需要一些更有趣的东西,你可能不得不考虑自己推出。同样,可能很难判断AppStore是否可以接受。