iphone - 关于带有文本字段的 UIAlertView...

标签 iphone objective-c

我有这段代码来提示 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/

相关文章:

ios - 陀螺仪驱动球体

iphone - 播放音频时 AVPlayer 在 StitchedStreamPlayer 示例中崩溃

objective-c - NSDictionary 和关于 "pointer from integer without a cast"的警告

iphone - iOS5:像通知中心一样跟踪手指并向下拖动 View Controller

ios - 在索引路径重新加载行不起作用

iphone - CFAttributedStringRef 中的内存泄漏

ios - 尝试设置 PFInstallation 的 "username"属性时发送到实例的无法识别的选择器

ios - 为导航栏设置色调颜色恢复为 iOS 6 蓝色

objective-c - 如果 NSMutableArray 被多个线程同时修改会怎样? (如果它包含的对象保留在别处)

iphone - iOS4中的 "Power Logging"设置有什么作用