I'm trying to implement inheritance with xib files. Yes, a little strange, but let me tell you why.
I have a class, SLBaseViewController that many of my view controllers inherit from. When I want a child view controller I create it in the usual way:
SLHomeViewController *controller = [[SLHomeViewController alloc] initWithNibName:@"SLHomeViewController" bundle:nil];
This works fine. SLHomeViewController is an SLBaseViewController (which is a UIViewController).
I'm doing this because I have other view controllers that I want to inherit SLBaseViewController behavior. In my case, I have a navigation UI widget that is common across my app, so a SLSceneViewControll inherits from SLBaseViewController also and both SLHomeViewController and SLSceneViewController both get the custom nav widget behavior.
The custom nav widget also has position information that is common across SLBaseViewControllers. So I implemented a poor man's way of doing xib inheritance.
@interface SLBaseViewController : UIViewController <SLNavBarViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UIView *navBarExtendedFPO;
and the inheritance is done in initWithNibName
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
Class callingClass = [self class];
Class slBaseViewControllerClass = NSClassFromString (SL_BASE_VC_CLASS_NAME);
if (callingClass != slBaseViewControllerClass) {
SLBaseViewController *controller = [[SLBaseViewController alloc] initWithNibName:@"SLBaseViewController" bundle:nil];
// now load all the properties by hand
self.navBarExtendedFPO = controller.navBarExtendedFPO;
}
}
return self;
}
If I create a SLHomeViewController is load the xib of a SLBaseViewController and then copies the interesting property from it. If initWithNibName detects it is loading a SLBaseViewController it just does nothing, preventing an infinite loop.
The problem is, of course, that the outlet properties are not set yet. So it just copies nil.
So when are these outlet properties set?
Or - is there a better way to do what I'm trying to do? It all seemed rosy until I copy the properties by hand. That seems pretty brittle to me.
(Note, I'm fine with iOS6-only solutions.)
It happens because of UIViewController's the lazy initialization.
The view of UIViewController will be loaded only after some one call the view property.
like that:
So, in your case, you can just call
controller.view
beforeself.navBarExtendedFPO = controller.navBarExtendedFPO;
To explaining view life cycle more clear, there is an example:
there is overridden methods in your SLBaseViewController,
self.label
is analog ofnavBarExtendedFPO
defined in XIB fileand there is method that creates SLBaseViewController
so, there is our logs: