关于带有Textfield的UIAlertView …

我有这个代码来提示与文本字段的UIAlertView:

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? 谢谢。

在alertView的方法中点击- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex只取文本字段的值,并用它进行所需的操作…. 。

这里修改了代码

 UITextField *myTextField; ... { UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog") message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; [myTextField setBackgroundColor:[UIColor whiteColor]]; [myAlertView addSubview:myTextField]; [myAlertView show]; [myAlertView release]; } .... - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"string entered=%@",myTextField.text); } 

对于iOS 5和更高版本您可以使用UIAlertView alertViewStyle属性。

请参考Hamed的答案

如果你想添加一个TextField到UIAlertView,你可以使用这个属性(alertViewStyle)作为UIAlertView:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; [alert release]; 

并在它的.h文件中添加UIAlertViewDelegate作为协议,并在.m文件中实现alertView:clickedButtonAtIndex委托方法:

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"%@", [alertView textFieldAtIndex:0].text); } 

我希望这个对你有用!

注意:“适用于iOS 5.0及更高版本”

从iOS 8开始, UIAlertView已被弃用,并支持添加UITextFieldUIAlertController

迅速

 let alert = UIAlertController(title: "Title", message: nil, preferredStyle: .alert) alert.addTextField { (textField) in // optionally configure the text field textField.keyboardType = .alphabet } let okAction = UIAlertAction(title: "OK", style: .default) { [unowned alert] (action) in if let textField = alert.textFields?.first { print(textField.text ?? "Nothing entered") } } alert.addAction(okAction) self.present(alert, animated: true, completion: nil) 

Objective-C的

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { // optionally configure the text field textField.keyboardType = UIKeyboardTypeAlphabet; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField *textField = [alert.textFields firstObject]; }]; [alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil]; 
 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login" message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alert.alertViewStyle=UIAlertViewStylePlainTextInput; [alert show]; 

你需要一个UITextfield的全局variables,你想在你的AlertView委托方法中检索值。我在博客中创build了一个关于“如何从XIB向UIAlertView添加UITextField”的主题。 你可以看看下面的链接。

http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hey Welcome" message:@"MSG" delegate:self cancelButtonTitle:@"Ok Ji" otherButtonTitles:nil]; UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(15.0, 70.0, 200.0, 25.0)]; [textField setBackgroundColor:[UIColor whiteColor]]; [alert addSubview:textField]; [alert show]; [alert release];