jack
jack

Reputation: 155

Unable to set the tags for uialertview

This is the UIAlertView I am using setting the tag as shown

 UIAlertView *alertViewDelete = [[UIAlertView alloc] initWithTitle:@"delete User"
                                                                        message:@"can't restore data after deletion"
                                                                       delegate:nil
                                                              cancelButtonTitle:@"delete"
                                                              otherButtonTitles:@"cancel", nil];
                alertViewDelete.tag = 97;
                [alertViewDelete show];

Using this code to read the button clicked

- (void)alertView:(UIAlertView *)alertView1 clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView1.tag == 97)
    {
    if(buttonIndex == 1)
    {
        NSLog(@"ok");
    }
        else
        {
            NSLog(@"bye");
        }
    }
}

iOS application objective C ... I'm going again and again over this code, can't find where or what I'm doing wrong.

Upvotes: 0

Views: 102

Answers (3)

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

you forget two things

Add UIAlertviewDelegate in your viewcontroller.h

set

 alertViewDelete.delegate = self;
 alertViewDelete.tag = 97;
 [alertViewDelete show];

Upvotes: 1

HannahCarney
HannahCarney

Reputation: 3631

Try this :)

UIAlertController *alertViewDelete = [UIAlertController alertControllerWithTitle:@"Delete User" message:@"can't restore data after deletion" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        //your NSLog
        NSLog(@"ok")
        [av dismissViewControllerAnimated:YES completion:nil];
    }];

    UIAlertAction *deleteUser = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
        //your NSLog
        NSLog(@"bye")
        [av dismissViewControllerAnimated:YES completion:nil];
        // whatever method you have that deletes user
        [self deleteUser];
        // tell your application where you want it to go
        UIStoryboard *storyBoard =[ UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *vc = [storyBoard instantiateInitialViewController];
        AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
        del.window.rootViewController = vc;
    }];

    [av addAction:cancel];
    [av addAction:deleteUser];

Upvotes: 0

Oscar Swanros
Oscar Swanros

Reputation: 19969

From one of WWDC '15 videos: avoid tags, use references.

Which means, set your alert view as a property on your class, that way you don't depend on a random int to know whom you're looking for.

As for your code snippet, you're setting the UIAlertViews delegate to nil. You have to conform to the UIAlertViewDelegate on your class and set the delegate to self for that delegate method to be called.

Upvotes: 0

Related Questions