开发者

About UIAlertView with Textfield...

开发者 https://www.devze.com 2023-01-14 21:46 出处:网络
I have this code to prompt the UIAlertView, with UITextfield: UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@\"New List Item\", @\"new_list_dialog\")

I have this code to prompt the UIAlertView, with 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 al开发者_StackOverflow中文版loc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];

But I would like to add a get the textfield value, after the user click "OK", and after the user click , I want to call a method, how can I assign that to the myAlertView? Thank you.


If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:

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

and in .h file of it add UIAlertViewDelegate as a protocol and implement the alertView:clickedButtonAtIndex delegate method in the .m file:

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

I hope, it works for you!

Note: "Available in iOS 5.0 and later"


Declare the text field as global.And in the method of alertView clicked - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex just take the value of the textfield and do the operations you want with it.....

Heres the revised code

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);
}

For iOS 5 and later You can use alertViewStyle property of UIAlertView.

Please refer Hamed's Answer for the same


As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields.

Swift

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


You need a global variable for the UITextfield, you want to retrieve value in your AlertView Delegate method.I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB". You can take a look at the following link.

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

精彩评论

暂无评论...
验证码 换一张
取 消