我有这段代码来提示 UIAlertView
,带有 UITextfield
:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")
message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];
但我想添加一个获取文本字段值,在用户单击“确定”后,在用户单击后,我想调用一个方法,如何将其分配给 myAlertView?谢谢你。
最佳答案
如果你想给 UIAlertView 添加一个 TextField,你可以为 UIAlertView 使用这个属性(alertViewStyle):
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
并在它的 .h 文件中添加 UIAlertViewDelegate 作为协议(protocol),并在 .m 文件中实现 alertView:clickedButtonAtIndex 委托(delegate)方法:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
希望对你有用!
注意:“适用于 iOS 5.0 及更高版本”
关于iphone - 关于带有文本字段的 UIAlertView...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634204/