【发布时间】:2016-07-21 14:40:29
【问题描述】:
【问题讨论】:
标签: ios objective-c alert
【问题讨论】:
标签: ios objective-c alert
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Text"
message:@"Enter some text below"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (alert.textFields.count > 0) {
UITextField *textField = [alert.textFields firstObject];
textField.text // your text
}
}];
[alert addAction:submit];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"something"; // if needs
}];
[self presentViewController:alert animated:YES completion:nil];
【讨论】:
要将 TextField 添加到 UIAlertView 将 alertViewStyle 属性设置为 UIAlertViewStylePlainTextInput
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
在 .h 文件中添加 UIAlertViewDelegate 作为协议,并在 .m 文件中实现委托方法alertView:clickedButtonAtIndex。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
【讨论】: