PocketTim
PocketTim

Reputation: 87

UIAlertView won't trigger method

I have an option in my iOS Newsstand application for users to delete all of the issues in the app to free up space. Initially one press of the button would delete everything without warning so I decided to add in an alert view to give them the chance to delete or cancel.

I have the alert working just how I want it but choosing "delete" doesn't trigger the method. Can anyone tell me why?

This is my code:

- (void)showAlert 
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
                                                    message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Delete", nil];
    [alert show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Is this my Alert View?
    if (alertView.tag == 100) {
        //Yes
        // You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons.
        // Then u can check which button was pressed.
        if (buttonIndex == 0) { 
            // 1st Other Button
        } else if (buttonIndex == 1) { 
            // 2nd Other Button
            [self performSelector:@selector(trashContent) withObject:nil afterDelay:0.01];
        }
    } else {
        //No
        // Other Alert View
    }
}

- (void)trashContent 
{
    if (NewsstandApp) {
        NKLibrary *nkLib = [NKLibrary sharedLibrary];
        NSLog(@"%@",nkLib.issues);
        [nkLib.issues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [nkLib removeIssue:(NKIssue *)obj];
        }];

        [self.publisher addIssuesInNewsstand];
    } else {
        for (Issue *issue in self.publisher.issues) {
            NSFileManager* fileManager = [NSFileManager defaultManager];
            NSError *error;
            [fileManager removeItemAtPath:issue.fileUrl error:&error];

            if (!error) {
                issue.downloadState = IssueStatusNone;
            }
        }
    }
    [self.collectionView reloadData];
}

Upvotes: 0

Views: 88

Answers (2)

Popeye
Popeye

Reputation: 12093

When you create your UIAlertView you never assign it a tag of 100 so when you do the check if (alertView.tag == 100) in clickedButtonAtIndex: it will always return FALSE and never make it to your second if statement where you determine which button was pressed. So change your showAlert too:

- (void)showAlert 
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete all issues?"
                                                    message:@"Your issues will be deleted to free up space on your device but can be re-downloaded as long as you are subscribed."
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Delete", nil];
    [alert setTag:100]; // Add this line.
    [alert show];
}

Personally I'd actually create a constant and assign it the constant and check that so have a constant like const int deleteAllAlertTag = 100; declared at the top of you implementation file then have [alert setTag:deleteAllAlertTag]; and then you can do if (alertView.tag == deleteAllAlertTag). I'd do this only because if you decide to change the value of your alertView tag you only have to change it once and your code will still work.

Upvotes: 2

Almo
Almo

Reputation: 15861

You need

alert.tag = 100;

in the code where you create it. If this is in fact the problem, then your question isn't why the method isn't tiggered, since it is. Using the debugger to step through your code would show the if failing to trigger because the tag isn't set to 100.

Upvotes: 1

Related Questions