在IOS 7中的UIAlertview iOS上的多个文本字段

所以新的iOS 7已经出来,我试图添加多个textFields和标签到UIAlertviews。 我需要三个。 我一直试图添加他们作为子视图,并不再工作。 我也尝试添加UIAlertViewStylePlainTextInput多行,但它似乎只返回一个文本字段。

我需要添加标签以向他们显示要input的内容。 有没有办法用新的iOS 7完成这个任务?

我发现在iOS7中使用UIAlertView与多个文本字段唯一的解决scheme是仅用于login。

使用这一行来初始化你的alertView

 [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 

而这个来抓取用户的input:

 user = [alert textFieldAtIndex:0].text; pw = [alert textFieldAtIndex:1].text 

除了login之外的其他目的, 在iOS7中查看UIAlertView的addSubview

您可以在iOS7的标准警报视图中将accessoryView更改为任何自定义的 customContentView

 [alertView setValue:customContentView forKey:@"accessoryView"]; 

请注意,您必须在[alertView show]之前调用它。

最简单的说明示例:

 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; v.backgroundColor = [UIColor yellowColor]; [av setValue:v forKey:@"accessoryView"]; [av show]; 

在这里输入图像说明

如果您只想向UIAlertView添加两个文本字段,则可以使用UIAlertViewStyleLoginAndPasswordInput并按以下方式修改文本字段:

 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Some Title" message:@"Some Message." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"No, thanks", nil]; alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert textFieldAtIndex:1].secureTextEntry = NO; //Will disable secure text entry for second textfield. [alert textFieldAtIndex:0].placeholder = @"First Placeholder"; //Will replace "Username" [alert textFieldAtIndex:1].placeholder = @"Second Placeholder"; //Will replace "Password" [alert show]; 

之后,在UIAlertView委托中,您可以简单地使用以下方法获取文本:

 text1 = [alert textFieldAtIndex:0].text; text2 = [alert textFieldAtIndex:1].text;