关于UIAlertView with Textfield ...

时间:2010-09-03 08:44:54

标签: 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?谢谢。

6 个答案:

答案 0 :(得分:201)

如果要将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及更高版本中可用”

答案 1 :(得分:30)

将文本字段声明为global.And在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及更高版本 您可以使用alertViewStyle的{​​{1}}属性。

请参阅Hamed的答案

答案 2 :(得分:19)

自iOS 8开始UIAlertView已弃用UIAlertController,这增加了对添加UITextField的支持。

夫特

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)

目标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];

答案 3 :(得分:2)

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login" message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
[alert show];

答案 4 :(得分:0)

你需要一个UITextfield的全局变量,你想要在AlertView Delegate方法中检索值。我在我的博客中创建了一个帖子,主题是“如何从XIB向UIAlertView添加UITextField”。您可以查看以下链接。

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

答案 5 :(得分:0)

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];