Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MickeyT

macrumors member
Original poster
Apr 26, 2010
92
0
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:

Code:
-(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.
 
Last edited:
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.
 

Attachments

  • Screen Shot 2013-02-02 at 12.33.18.png
    Screen Shot 2013-02-02 at 12.33.18.png
    30.7 KB · Views: 126
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.