检查单击了哪个UIAlertView

时间:2014-09-29 16:15:34

标签: ios objective-c cocoa-touch uialertview

您好我写了以下代码:

    - (IBAction)DelBlockB:(id)sender {
    confirmDelB = [[UIAlertView alloc] initWithTitle:@"Attention" message:[NSString stringWithFormat:@"are you sure you want to delete and block %@", idnameArr[2]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    [confirmDelB show];
}

- (IBAction)DelFB:(id)sender {
    confirmDel = [[UIAlertView alloc] initWithTitle:@"Attention" message:[NSString stringWithFormat:@"are you sure you want to delete %@", idnameArr[2]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];

    [confirmDel show];
}

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

    if (buttonIndex == 0) {
        NSLog(@"ok");
    }
    else {
        NSLog(@"cancel");
    }
}

方法" clickedButtonAtIndex"无论UIAlertView是什么按下,都会返回答案, 如果单击其中一个警报,我该如何显示答案?

4 个答案:

答案 0 :(得分:2)

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

UIAlertView的委托方法,您无法更改每个警报视图调用的方法。但是,没有什么可以阻止您检查警报视图(它毕竟是传入的)。

在每个提醒视图中......

阅读过去。我真的不喜欢使用像这样的标签属性。

confirmDelB.tag = 1;
confirmDel.tag = 2;

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) {
        // This is confirmDelB.
    } else {
        // This is confirmDel.
    }
}

这是一个更优雅的解决方案。

但是,我 HATE 使用了标签。我会(可能)做的是创建一个alertView属性。

@property (nonatomic, strong) UIAlertView *deleteAndBlockAlertView;
@property (nonatomic, strong) UIAlertView *deleteOnlyAlertView;

然后使用类似......

的方法
- (UIAlertView *)deleteAndBlockAlertViewWithObject:(id)object
{
    if (!_deleteAndBlockAlertView) {
        confirmDelB = [[UIAlertView alloc] initWithTitle:@"Attention"
                                                 message:@""
                                                delegate:self
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:@"Cancel", nil];
    }

    _deleteAndBlockAlertView.message = [NSString stringWithFormat:@"are you sure you want to delete and block %@", object];

    return _deleteAndBlockAlertView;
}

另一方也一样。

现在你可以像......那样展示它。

- (IBAction)delBlockB:(id)sender
{
    [[self deleteAndBlockAlertViewWithObject:idnameArr[2]] show];
}

在委托方法中......

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView == self.deleteAndBlockAlertView) {
        // This is confirmDelB.
    } else {
        // This is confirmDel.
    }
}

在我看来,这是一个更优雅的解决方案。而且您只需要创建一次警报视图。

答案 1 :(得分:1)

为每个alertView添加tag并检查委托调用中的tag值。

- (IBAction)DelBlockB:(id)sender 
{
    confirmDelB = [[UIAlertView alloc] initWithTitle:@"Attention" message:[NSString stringWithFormat:@"are you sure you want to delete and block %@", idnameArr[2]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    confirmDelB.tag = 666;

    [confirmDelB show];
}

- (IBAction)DelFB:(id)sender {
    confirmDel = [[UIAlertView alloc] initWithTitle:@"Attention" message:[NSString stringWithFormat:@"are you sure you want to delete %@", idnameArr[2]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
    confirmDelB.tag = 667;

    [confirmDel show];
}

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

    if (alertView.tag == 666) {
        // Do something
    }
    else {
        // Do something else
    }
}

答案 2 :(得分:1)

在显示提醒之前

 myAlertView.tag = 0 // different number for different alertView

内部clickedButtonAtIndex:

if (alertView.tag == 0){
  //alert zero stuff and buttonIndex if
}
if (alertView.tag == 1){
  //alert one stuff and buttonIndex if
}

答案 3 :(得分:1)

使用块是委托+标签的首选方法,这两种方法都很糟糕且过时。请考虑使用以下方法。

//Could be a category method!
- (void)presentAttentionAlertWithOkayBlock:(CompletionBlock)okayBlock {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attention" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"Cancel"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action) {
                                       NSLog(@"Cancel action");
                                   }];
    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:@"OK"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action) {
                                   NSLog(@"OK action");
                                   if (okayBlock) okayBlock();
                               }];

    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

这样,当用户点击“okay”时,可以明确区分每个警报的作用。此外,阅读您的代码的人不必去寻找那种代表方法来查看警报会做什么!

- (IBAction)DelBlockB:(id)sender {
    [self presentAttentionAlertWithOkayBlock:^{
        //Do something
    }];
}

- (IBAction)DelFB:(id)sender {
    [self presentAttentionAlertWithOkayBlock:^{
        //Do something else
    }];
}