user3780061
user3780061

Reputation: 65

Check which UIAlertVeiw was clicked

Hi I wrote the following code:

    - (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");
    }
}

where the method "clickedButtonAtIndex" will return an answer no matter what UIAlertView was Pressed, how can I show an answer only if one of the alert was clicked ?

Upvotes: 1

Views: 101

Answers (4)

CrimsonChris
CrimsonChris

Reputation: 4641

Using blocks is a preferred approach to delegates+tags, both of which are terrible and outdated. Consider using the following method.

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

This allows a clear separation between what each alert is going to do when the user clicks "okay". Also, people reading your code don't have to go hunting down that darn delegate method to see what the alert will do!

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

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

Upvotes: 1

Fogmeister
Fogmeister

Reputation: 77651

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

is a delegate method for UIAlertView you can't change which method is called per alert view. However, there is nothing stopping you from inspecting the alert view (it is passed in after all).

On each alert view...

Read past this. I really don't like using the tag property like this.

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

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

This is a much more elegant solution.

However, I HATE the use of tag. What I would do (possibly) is create an alertView property.

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

Then use a method like...

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

And the same for the other.

Now you can show it like...

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

And in the delegate method...

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

This is a much more elegant solution in my opinion. And you only ever need to create each alert view once.

Upvotes: 2

Totka
Totka

Reputation: 627

before show your alert

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

inside clickedButtonAtIndex:

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

Upvotes: 1

Alfie Hanssen
Alfie Hanssen

Reputation: 17104

Add a tag to each alertView and check the tag value inside the delegate call.

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

Upvotes: 1

Related Questions