2014-09-29 81 views
1

您好我写了下面的代码:检查其UIAlertVeiw被点击

- (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中被压, 我怎么能显示答案只有一个警报被点击了?

+1

使用新的[UIAlertController](https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIAlertController_class/index。HTML#// apple_ref/OCC/instm/UIAlertController /的addAction :)。它可以让你添加行动,而不是布线代表。 – CrimsonChris 2014-09-29 16:19:12

+0

@CrimsonChris好的,太棒了。我不知道,谢谢。不过,如果你想支持iOS7,我的答案仍然有效:D – Fogmeister 2014-09-29 16:28:19

+0

如果你的子类UIAlertView是iOS7,你仍然可以支持块。 – CrimsonChris 2014-09-29 16:29:01

回答

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 
    } 
} 
+0

太好了,谢谢。 – user3780061 2014-09-29 16:29:17

+2

很高兴这有帮助。关于这里关于标签等的争论已经过时,将警报存储在属性中,使用块。标记方法是最简单的方法,需要最少的附加代码/复杂性。而他们“过时”的想法并不合理。它们一直存在,但它们不被弃用或不被赞同 - 它们只是为了这个用例而设计的。 – 2014-09-29 16:34:33

+0

该解决方案不能很好地扩展。在这个微不足道的情况下,很难逻辑地跟随任何更复杂的事情。 – CrimsonChris 2014-09-29 16:45:12

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 
} 
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. 
    } 
} 

这在我看来是一个更好的解决方案。你只需要创建每个警报视图一次。

+0

如果您在代表中没有分支机构会更好。如果您有大量可能的警报视图,它会变得非常复杂。 – CrimsonChris 2014-09-29 16:47:28

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]; 
} 

这使得当用户点击“好”时每个警报将要做的事情之间清晰分离。此外,阅读代码的人员不必去寻找代理方法来了解警报的功能!

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

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

“可怕和过时”是主观和极端的。代表们很糟糕/过时?标签是解决这个问题的最轻量级方法。只要它们不被废弃,就没有害处。 – 2014-09-29 16:38:17

+0

UIAlertView现在不推荐使用,而是替换为使用块的解决方案。 – CrimsonChris 2014-09-29 16:40:50

+0

如果代表和标签是“可怕的和过时的”,那么Objective-C也是如此,你应该在Swift中做所有事情:P但是,+1指出了一个我不知道的令人敬畏的新iOS8事物。 – Fogmeister 2014-09-29 16:40:57