Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView input prompt [duplicate]

I require an input prompt in my app and I've tried this

// Create a new item
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];

And then handling it like this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// The user created a new item, add it
if (buttonIndex == 1) {
    // Get the input text
    NSString *newItem = [[alertView textFieldAtIndex:0] text];
}
}

but it doesn't look like the clickedButtonAtIndex gets called, why?

Kind Regards, Erik

like image 904
Erik Avatar asked May 18 '14 13:05

Erik


1 Answers

You need to set the delegate.

alert.delegate = self; //Or some other object other than self

Or when you initialise the alert:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" 
                                                message:@"Enter a name for the item"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Add", nil];
like image 70
lucianomarisi Avatar answered Sep 18 '22 05:09

lucianomarisi