Resolved Simulate memory warning not working

Discussion in 'iOS Programming' started by MickeyT, Feb 2, 2013.

  1. MickeyT, Feb 2, 2013
    Last edited: Feb 2, 2013

    MickeyT macrumors member

    Apr 26, 2010
    Newcastle, United Kingdom
    I have a UITabBarController containing two view controllers.

    The purpose of an exercise I am following is to demonstrate that you should never reference a view controller's view in the initialiser of a view controller.

    To demonstrate the point, I am asked to add the following code to the view controller's initWithNibName method:

    -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        self = [super initWithNibName:nil bundle:nil];
        if (self) {
            UITabBarItem *tbi = [self tabBarItem];
            [tbi setTitle:@"Time"];
            UIImage *i = [UIImage imageNamed:@"Time"];
            [tbi setImage:i];
            //This is the offending line
            [[self view] setBackgroundColor:[UIColor greenColor]];
        return self;
    I am then asked to go to the simulator, select the "Time" tab, go back to the other tab and then simulate a memory warning. Then, when I return to the "Time" tab, the background is supposed to not be green anymore because only the viewDidLoad method is called in this situation, not the initialiser.

    However, when I do this the viewController's view doesn't seem to get destroyed because the background is still green.

    I am also directed to override the viewDidUnload method, and in here an NSLog instruction is supposed to write something to the console. However, it never does, which indicates that viewDidUnload is never getting called.

    For information, the memory warning simulation does seem to do something because "Received memory warning" appears in the console.

    Does anyone know why this is, please? I am fairly confident I've followed the book to the letter.

    Could it be something to do with iOS 6? The book was written when iOS 5 was the latest OS version. I can't seem to test this theory in the simulator because I inadvertently built the app as an iOS 6 app, and the new layout constraints that appear as part of building the XIB file cause the iOS 5 simulator to crash.

    Thank you.
  2. MickeyT thread starter macrumors member

    Apr 26, 2010
    Newcastle, United Kingdom
    I've helped myself here....I must have been looking at the book when I typed this out the first time and missed the popup in the attached screenshot.

    Please consider this one resolved.

    Attached Files:

Share This Page