developer110
developer110

Reputation: 508

Dismissing Modal View Controller

I have a root view controller, which serves as a menu. When an item is selected it presents some full-screen data modally. When the back button is hit, the following code is executed:

In BoardViewController.m:

 - (IBAction)menuButtonPressed:(id)sender
    {
         [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }

It brings back nicely to the main menu. But after this I'd like to the dismissed view controller to be destroyed (like when you're using push/pop view controllers). I don't store any references of them, but they are still alive after dismissing. How can I fix it? (Using ARC.)

EDIT

In AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    MenuViewController *menuVC = [[MenuViewController alloc] init];
    self.window.rootViewController = menuVC;
    ...
}

In MenuViewController.m:

- (IBAction)newGame:(id)sender
    {
        BoardViewController *boardVC = [[BoardViewController alloc] init];
        boardVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:boardVC animated:YES completion:nil];
    }

EDIT 2

Well, a non-weak delegate property caused the problem. Thanks for all!

Upvotes: 2

Views: 5750

Answers (3)

Mike M
Mike M

Reputation: 4436

I don't use ARC, but if the modal controller isn't being freed, then it's probably because something else still has a reference to it. Does the modal controller add itself as a delegate to anything?

Upvotes: 3

Totumus Maximus
Totumus Maximus

Reputation: 7573

presenting a ModalViewController should look something like this in code:

- (void)showModal
{
    MyModalVC *mmvc = [[MyModalVC alloc] init];

    mmvc.dismissDelegate = self;

    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:mmvc];

    navController.modalPresentationStyle = UIModalPresentationFormSheet; //or similar

    [self presentModalViewController:navController animated:YES];

    [cleaningTaskVC release]; //see that it is released after the presentation so that when you dismiss it you don't have to worry about the destruction of the object anymore
    [navController release];
}

The releases at the end will ensure the destruction so that you don't have to worry about it when you dismiss it.

This is how I dismiss it (with the protocol and delegate I use from within the ModalVC class) and afterwards there is no instance alive of the ModalVC

- (void)didDismissModalView
{
    [self dismissModalViewControllerAnimated:YES];
}

Hopefully this is what you want.

Good luck.

Upvotes: 2

Krunal
Krunal

Reputation: 6490

try this,

- (IBAction)menuButtonPressed:(id)sender
{
   [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 1

Related Questions