关于UIAlertView with Textfield ...

DNB*_*ims 37 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];
Run Code Online (Sandbox Code Playgroud)

但我想添加一个获取文本字段值,用户点击"确定"后,用户点击后,我想调用一个方法,如何将其分配给myAlertView?谢谢.

Ham*_*abi 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];
Run Code Online (Sandbox Code Playgroud)

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

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

我希望这个对你有用!

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


Sur*_*rma 30

将文本字段声明为全局.在单击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);
}
Run Code Online (Sandbox Code Playgroud)

对于iOS 5及更高版本,您可以使用alertViewStyle属性UIAlertView.

请参考Hamed的答案

  • 抱歉dat ...我已经改变了我的答案@binnyb ...希望它的好... (3认同)

Ric*_*tos 20

截至iOS 8 UIAlertView已被弃用UIAlertController,这增加了对添加UITextFields的支持.

迅速

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)
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)