This is the scenario: A simple app with two viewcontroller classes and a storyboard with 1 viewcontroller. ViewController 'Super' inherits from UIViewcontroller
and implements -loadView
and the other view controller, 'Sub', inherits from 'Super'. The view controller class in the storyboard is 'Sub'.
In code:
@implementation SOSuperViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view.backgroundColor = [UIColor redColor];
NSLog(@"%@=> %@", NSStringFromClass([self superclass]), NSStringFromSelector(_cmd));
}
@end
@implementation SOSubViewController
- (void)awakeFromNib {
//Outlets are lazy loaded so self.view it is not really set here
[super awakeFromNib];
NSLog(@"%@=> %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
@end
The class for view controller in the storyboard is SOSubViewController
. When executing the app you can see -awakeFromNib
invoked in the 'Sub' controller and later -loadView
invoked in the 'Super' controller and the result is that the screen is red as set in -loadView
in the 'Super'.
The first question is: Is there any way to avoid -loadView
in 'Super' to be called letting the 'Sub' to get the view from the storyboard?
If using xibs instead storyboards I would implement -loadView
in 'Sub' to get the view from the xib file, something like:
- (void)loadView {
self.view = [[[NSBundle mainBundle] loadNibNamed:@"SOView" owner:self options:nil] firstObject];
}
But I can't know the name of the nib file using storyboards, instead I would need to uses nibBundle and nibName properties if set:
- (void)loadView {
if (self.nibBundle && self.nibName) {
[self.nibBundle loadNibNamed:self.nibName owner:self options:nil];
}
}
But that crashes:
2014-04-10 11:55:11.872 loadViewTest[8941:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/nlg/Library/Application Support/iPhone Simulator/7.1/Applications/E36DFC3A-4222-4CE4-A954-CC1C3891DCE1/loadViewTest.app> (loaded)' with name 'vXZ-lx-hvc-view-kh9-bI-dsS''
*** First throw call stack:
(
0 CoreFoundation 0x017ec1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156b8e5 objc_exception_throw + 44
2 CoreFoundation 0x017ebfbb +[NSException raise:format:] + 139
3 UIKit 0x004e0b7b -[UINib instantiateWithOwner:options:] + 951
4 UIKit 0x004e2ada -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 165
5 loadViewTest 0x00002cff -[SOSuperViewController loadView] + 319
6 UIKit 0x0034a0d3 -[UIViewController loadViewIfRequired] + 78
7 UIKit 0x0034a5d9 -[UIViewController view] + 35
8 UIKit 0x0026a267 -[UIWindow addRootViewControllerViewIfPossible] + 66
9 UIKit 0x0026a5ef -[UIWindow _setHidden:forced:] + 312
10 UIKit 0x0026a86b -[UIWindow _orderFrontWithoutMakingKey] + 49
11 UIKit 0x002753c8 -[UIWindow makeKeyAndVisible] + 65
12 UIKit 0x00225bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
13 UIKit 0x0022a667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
14 UIKit 0x0023ef92 -[UIApplication handleEvent:withNewEvent:] + 3517
15 UIKit 0x0023f555 -[UIApplication sendEvent:] + 85
16 UIKit 0x0022c250 _UIApplicationHandleEvent + 683
17 GraphicsServices 0x037e1f02 _PurpleEventCallback + 776
18 GraphicsServices 0x037e1a0d PurpleEventCallback + 46
19 CoreFoundation 0x01767ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
20 CoreFoundation 0x017679db __CFRunLoopDoSource1 + 523
21 CoreFoundation 0x0179268c __CFRunLoopRun + 2156
22 CoreFoundation 0x017919d3 CFRunLoopRunSpecific + 467
23 CoreFoundation 0x017917eb CFRunLoopRunInMode + 123
24 UIKit 0x00229d9c -[UIApplication _run] + 840
25 UIKit 0x0022bf9b UIApplicationMain + 1225
26 loadViewTest 0x0000309d main + 141
27 libdyld.dylib 0x01e33701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The second question is: Is it possible to load a nib using storyboards without any xib file?
As curiosity, I just discovered the value of self.nibName is vXZ-lx-hvc-view-kh9-bI-dsS with is #object id for the viewcontroller in storyboard#-view-#object id for the view in the viewcontroller# which kind of look right.