简体   繁体   中英

What does the back button in a navigation controller do that pushViewController doesn't?

When testing my app using Instruments -> Activity Monitor, I'm seeing a difference in memory use when a transition is done via the back button and follows the navigation controller vs calling a method in AppDelegate that uses pushViewController . If the navigation is completed by the navigation back button, then the memory use drops (I'm new to iOS programming, but I believe the term is that it's releasing the subviews of that particular controller). If I navigate away from that view controller by using a method in AppDelegate that uses pushViewController , then the memory isn't released and if you go back to that view controller, it starts adding up again. I'm working in XCode 4.2 writing for iOS5.

This is the method in AppDelegate:

-(void)applicationDidTimeout:(NSNotification *) notif
{
    UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"slideShow"];

    [[UIScreen mainScreen] setBrightness:0.0];
        NSLog(@"go home");
        [(UINavigationController *)self.window.rootViewController pushViewController:controller animated:YES];
}

The app flows like this: Main Page with 4 buttons. Button 1 pushes a UIViewController with a scrollview which has a subview (A). That subview also has a subview (B) with a close button. Hit the close button and subview(B) is removed from superview. Hit the back button on the top left and it returns to the Main Page. If you have no user interaction after a set period of time, the method above fires and send you to screenSaverViewController. Touch anywhere in the screen saver and you go back to Main Page.

When watching the Activity Monitor, the amount of memory being reported as used in the Real Memory Usage window increases every time a subview(B) appears, but doesn't go down until you hit the back button and return to Main Page. If the screen saver kicks in, then it doesn't go down at all.

Using either method, viewWillDisappear and viewDidDisappear both fire in the scrollview Controller. Why does one release memory and the other doesn't?

All of my navigations work, it's just a memory usage issue that I need assistance with.

Follow Up:

picciano pointed out what I wasn't seeing - pushViewController does what it does. Pushes views on top of existing views. Can anyone suggest the proper replacement for pushViewController? The method has to fire from AppDelegate so that the app navigates from any view to the screen saver when the notification fires.

Solved:

What I ended up doing was moving the navigation from AppDelegate to the individual view controllers. I made sure to #import "Timer.h" and added my notification listener to viewDidLoad [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil]; and then added the applicationDidTimeout: method:

-(void)applicationDidTimeout:(NSNotification *) notif
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

1, in this case, is my screen saver controller. 0 = the root view. These numbers follow the navigation stack. root view (0) -> screen saver (1) -> main page (2) -> 1 of 4 other pages

pushViewController (like the name implies) add a new instance of a UIViewController into the navigation controller, increasing overall memory usage.

The back button by contrast removes the top UIViewController from the navigation controller and releases memory.

If you continue pushing view controller after view controller, you will eventually run out of memory and crash.

Rethink your navigation.

Another potential solution is to have a look at some of the UIView methods for adding, removing, or exchanging subviews.

When thinking about navigation, sometimes good old fashioned paper and pencil sketches are useful to diagram your navigation model.

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