Getting text from UIAlertView

社会主义新天地 提交于 2019-11-30 07:06:52

问题


I'm trying to get text from an alert view and add it to my mutable array to list in a table view. I realize there is a similar question that was posted a few months ago, but I dont understand how to utilize the given answer.

-(IBAction)insert {
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Enter Name"];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];

    UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    [dialog addSubview:nameField];
    [dialog show];

    [data addObject:[nameField text]];
    [mainTableView reloadData];

However my app crashes because it says I'm attempting to insert a nil object at index 0. What am I doing wrong?

EDIT: Ok I think I'm missing a method to handle the alertview. So I found this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:@"Cancel"]) {
        return;
    }
    else if([buttonTitle isEqualToString:@"Ok"]) {
        [data addObject:nameField.text];

    }

Now I just need to connect the pieces, but not sure how.


回答1:


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"
    }
}



回答2:


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



回答3:


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.




回答4:


– 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.




回答5:


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.



来源:https://stackoverflow.com/questions/4560346/getting-text-from-uialertview

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