UIAlertView委托 - 返回调用方法的按钮索引

时间:2013-12-04 18:31:01

标签: ios objective-c uialertview uialertviewdelegate

不确定我是否正确地提出了这个问题。有没有办法让UIAlertView中选择的按钮索引返回到启动UIAlertView的方法?

所以

- (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) {
    }

}

2 个答案:

答案 0 :(得分:0)

不,您不能向显示警报视图的方法“返回”任何内容,因为该方法已完成执行。如果要对特定按钮执行操作,请单击,然后调用新方法。

    if (buttonIndex == 0) {
      [myclass someNewMethod];
    }

答案 1 :(得分:0)

您可以使用来自BlocksKit

的便捷类别UIAlertView + BlocksKit.h
- (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);

                                   }];
}