Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController not loading my custom UIView

Tags:

cocoa-touch

This should be straight forward for a guru. I don't have any code really written out, just a couple of controllers and a custom UIView. All connected through nibs. The app loads without crashing, yet I can't see my NSLog() hit from my custom UIView.

My application delegate has default template code which calls for a class of mine called TabAnimationController. TabAnimationViewController has its view set to TabView. I made sure that in TabAnimationViewController's NIB that File's owner is set to TabAnimationViewController and that my instance of UIView has its class set to TabView.

In TabView.m I'm trying to see how NSLog is going to hit, and it's not showing up at all.

- (void)loadView {
 NSLog(@"calling loadView");
}

- (id)initWithFrame:(CGRect)frame {
NSLog(@"Calling initWithFrame:");
    return self;
}

Strange. I'm not sure why even after proper IB connections that my NSLog will not show up. Only anything put into drawRect: will invoke. Why isn't initWithFrame or loadView ever get hit? What if I want to customize this view programmatically?

like image 298
Coocoo4Cocoa Avatar asked Mar 02 '23 03:03

Coocoo4Cocoa


1 Answers

First of all, when a view is dehydrated from nib file, instead of initWithFrame, initWithCoder is invoked. So you need to implement your initialization in initWithCoder as well. (It may be a good idea to keep the initWithFrame initialization as well, if you anticipate programmatically creating your TabView instead of hooking up in the IB. Just refactor your initialization to another method and call it from both implementations.)

Also in your initialization code above you must always call the super class's initialization. There is a boiler plate pattern all custom classes use in their init implementation for that:

if (self = [super initXXX]) { do your initialization } 
return self;

Second, loadView which is actually a UIViewController method and not a UIView method is invoked only if the view outlet of the controller is nil.

Unless you are composing your view yourself programmatically using your controller, you do not need to override loadView. Instead you should override viewDidLoad, which is called after the view is loaded, to do additional initialization.

like image 163
keremk Avatar answered Apr 26 '23 05:04

keremk