-1

Not sure if I worded that question correctly. Is there a way to have the button index selected in a UIAlertView returned to the method where the UIAlertView was initiated in?

So

- (void) someMethod { 

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirm Adding Product" message:Blah, blah, blah" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes", @"No", nil];
    [alert show];

//some more code - I'd like to get the button index returned here!

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

   //anyway to return this to the method above?
    if (buttonIndex == 0) {
    }

}
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • No, that is why we use delegation, to delegate a certain task to another method/place. What are you trying to accomplish within `someMethod` that you cannot do in the delegate method? – Peter Foti Dec 04 '13 at 18:33
  • 1
    Pass a value to a third method... – PruitIgoe Dec 04 '13 at 19:39

2 Answers2

0

No, you cannot "return" anything to the method that has shown the alert view because that method has finished executing. If you want to perform an action on a specific button click, then call a new method.

    if (buttonIndex == 0) {
      [myclass someNewMethod];
    }
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
0

You can use convenient category UIAlertView+BlocksKit.h from BlocksKit

- (void) someMethod {

    [UIAlertView bk_showAlertViewWithTitle:@"Title"
                                   message:@"Message"
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil
                                   handler:^(UIAlertView *alertView, NSInteger buttonIndex) {

                                       // Use buttonIndex in someMethod's context:
                                       NSLog(@"Pressed button at index %d", buttonIndex);

                                   }];
}
nalexn
  • 10,615
  • 6
  • 44
  • 48