7

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

nobody
  • 19,814
  • 17
  • 56
  • 77
Erik
  • 799
  • 2
  • 15
  • 27
  • oh, haha - sorry. I'm so used to asking when developing for windows phone so it's just an old habit – Erik May 18 '14 at 14:06
  • Ohh. Is it really necessary to downvote all the time? Now I can't ask questions... I'm asking because I haven't found the solution myself – Erik May 18 '14 at 14:08
  • I just had a look at the most recent questions of yours: They all leave the impression that you don't invest much time in teaching yourself. you don't seem to consult documentation and when advised to work yourself through some tutorials, you answer that they don't fit your project. But stackoverflow isn't a tool to let other people do your work. change your mindset or live with the down votes. – vikingosegundo May 18 '14 at 15:04
  • I'm sorry you got that impression and I certainly know that's not the purpose of stackoverflow. Nobody I know, know how to code and I'm not attending a school for that - so it isn't that easy to learn languages yourself. I'm trying to learn them, slowly but certain - and if I can't find the solution, I ask a question - like you would if you were in a coding school – Erik May 18 '14 at 15:25
  • how about searching for the answer first? duplicate questions are pollution. and this question is a good example for one that could be easily soled by searching. – vikingosegundo May 18 '14 at 15:49

2 Answers2

7

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];
lucianomarisi
  • 1,552
  • 11
  • 24
0

Method is not being called because you aren't set delegate .
pass self as delegate so it will get the reference to it.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95