Getting text from UIAlertView

廉价感情. 提交于 2019-11-29 01:23:44

A common mistake that people make when using a UIAlertView is that they think that sending it a show message will block the main thread. It does not. Your code continues to execute, even though there is an alert on the screen. Thus, no value exists for the UITextField that you have passed in.

What you need to do is implement the UIAlertViewDelegate in your UIViewController to grab whatever a user has entered into the text field. That said, you still have to check for a nil value, because if you don't type anything in, the text value will be nil.

UPDATE: This answer was posted before Apple created an API for adding a UITextField to an alert through the UIAlertViewStyle. Here is the updated code, borrowed from @matt

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

Then, in the UIAlertViewDelegate call back:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        // Insert whatever needs to be done with "name"
    }
}

You don't need to add your own text field to the AlertView but instead set the appropriate style

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

Then access the entered text after OK (button 1) was pressed

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        // name contains the entered value
    }
}
Ganesh NA

Don't try to add a UITextView on a UIAlertView or else Apple might reject your app.

I already used the same kind of functionality in my app. But Apple REJECTED MY APP due to the use of UITextView in UIAlertView.

– alertView:clickedButtonAtIndex: is a delegate method of UIAlertView. Just add the method you defined in your edit to your controller implementation and you should be good to go, since you already set the controller to be the AlertView's delegate.

Also add to your class header, to avoid any delegate related warnings, ie:

@interface MyViewController : UIViewController <UIAlertViewDelegate> {
...
};

Don't forget to remove [data addObject:[nameField text]]; and [mainTableView reloadData]; from the insert method.

Raxit
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert title" 
                                                  message:@"alert message" 
                                                 delegate:self 
                                        cancelButtonTitle:@"Cancel" 
                                        otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:nil label:@"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;               

No need to add a custom textfield. You can directly add using addTextFieldWithValue method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!