Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class.

I'm building the blog app from scratch and NSLog will NOT output to the debug area from the ViewController class but WILL output from the AppDelegate class.

To make sure I hadn't done something stupid, I started a band new Xcode project from scratch, added the ViewController class and then tried to get NSLog statements to output. Once again, NSLog outputs fine from the AppDelegate, but will not from ViewController or Main. I tried NSLog statements in various locations.

I then created another new project from the template and found that NSLog DOES output from the ViewController class. I compared the code in the Template version to the scratch version line-by-line, class-by-class and couldn't find any culprits.

Thanks for your help!

2 Answers

If you're creating an empty project (and manually creating a view controller class), you need to actually instantiate that class in the app delegate for any code in the class's viewDidLoad method to execute:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    MYViewController *myViewController= [MYViewController alloc] init];
    self.window.rootViewController = myViewController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

If you create an app from the template, the default view controller is already instantiated and set as root view controller, so the code inside the view controller class's viewDidLoad method is executed.

Thanks Dino. Your answer was very informative and helps me better understand the viewDidLoad method. However, I discovered that my true blunder was that the Custom Class name in the StoryBoard for my Table View did not match the class name for .m and .h TableViewController.

Thanks again Dino!

Thanks Dino. Your answer was very informative and helps me better understand the viewDidLoad method. However, I discovered that my true blunder was that the Custom Class name in the StoryBoard for my Table View did not match the class name for .m and .h TableViewController.

Thanks again Dino!