UIAlertController

This must be trivial, but I am perplexed! I have an app, which was working perfectly until I Included UIAlertController to launch alerts in lieu of UIAlertView, when the iOS version >=8, as in the following code. I have made no other changes. Now, only when UIAlertController is executed, the ViewController which contains this code is no longer dismissed, creating a second instance when I launch the same ViewController again. It seems as though the UIAlertController windows are not being dismissed properly and are still being referenced, though they are no longer visible. Can someone please tell me what I have not understood? Thanks!



if (versionFloat < 8.0)

{

_finishedAlert = [[UIAlertView alloc] initWithTitle:@“My Project”

message:@"Finished"

delegate:self

cancelButtonTitle:nil

otherButtonTitles:@"OK", nil];

[_finishedAlert show];

}

else

{

alertPresentation = [UIAlertController alertControllerWithTitle:@"My Project"

message:@"Finished"

preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK"

style:UIAlertActionStyleDefault

handler:^(UIAlertAction * action)

{

[self handleFinishedAlert];

}];

[alertPresentation addAction:actionOk];

[self presentViewController:alertPresentation animated:YES completion:nil];

}

Accepted Answer

Why are you using a global scope for alertPresentation? Remove the other references to this and use only a local variable. That way ARC will release it more quickly. Specifically:

change:

    alertPresentation = [UIAlertController alertControllerWithTitle:@"My Project"
                                                            message:@"Finished"
                                                     preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action)
                               {
                                   [self handleFinishedAlert];
                               }];
    [alertPresentation addAction:actionOk];
    [self presentViewController:alertPresentation animated:YES completion:nil];

to:

    UIAlertController *NEWalertPresentation = [UIAlertController alertControllerWithTitle:@"My Project"
                                                            message:@"Finished"
                                                     preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action)
                               {
                                   [self handleFinishedAlert];
                               }];
    [NEWalertPresentation addAction:actionOk];
    [self presentViewController:NEWalertPresentation animated:YES completion:nil];

Thanks PBK; this fixed the problem. You obviously detected that I'm a newbie. I appreciate your patient and detailed reply!

Newbie indeed - you even checked the wrong answer as the right answer.

UIAlertController
 
 
Q