简体   繁体   中英

UIViewController doesn't get deallocated after being popped from a navigation controller, if the action starts from a UIAlertView delegate

would you please look at that piece of code:

/* This app is a game, the user can click an "abort" button anytime,
 * and he/she is therefore asked for confirmation ("really abort game?")
 */
- (IBAction)btnAbortClicked:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setMessage:@"Really abort game?"];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"Yes"];
    [alert addButtonWithTitle:@"No"];
    [alert show];
    [alert release];
}

/* Delegate method (I don't like it, I wish I had modal blocking windows) */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
    [self quitGame];
}

/* pop the view controller */
- (void)quitGame {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

The problem is simple - but apparently not enough for me to solve. The UIViewController gets popped, but doesn't get deallocated. And the problem is strictly related to the UIAlertView, because if I just call quitGame from btnAbortClicked:, the view controller is popped and immediately deallocated.

Instead, it seems some mysterious entity retains it.

Can you help me? Thanks in advance.

Well, I think that you're still inside the alertView when clickedButtonAtIndex is called. I'd suggest moving to alertView:disDismissWithButtonIndex instead, so that you're called after the alertview disappears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM