-1

不确定我是否正确地用了这个问题。有没有办法让 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) {
    }

}
4

2 回答 2

0

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

    if (buttonIndex == 0) {
      [myclass someNewMethod];
    }
于 2013-12-04T18:33:15.857 回答
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);

                                   }];
}
于 2013-12-04T19:21:29.200 回答