这是场景:具有两个视图控制器类的简单应用和一个具有 1 个视图控制器的情节提要。ViewController 'Super' 继承自 UIViewcontroller
并实现 - loadView
和另一个视图控制器 'Sub' 继承自 'Super'。情节提要中的视图控制器类为"Sub"。
在代码中:
@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
情节提要中视图控制器的类是 SOSubViewController
。执行应用程序时,您可以看到 - awakeFromNib
"子"控制器中调用,稍后在"超级"控制器中调用loadView
,结果是屏幕是红色的,如在"超级"中设置loadView
。
第一个问题是:有什么办法可以避免 - loadView
在"超级"中被称为让"Sub"从故事板获取视图?
如果使用 xibs 而不是情节提要,我会实现 - loadView
在"Sub"中从 xib 文件中获取视图,如下所示:
- (void)loadView {
self.view = [[[NSBundle mainBundle] loadNibNamed:@"SOView" owner:self options:nil] firstObject];
}
但是我无法使用情节提要知道 nib 文件的名称,相反,如果设置,我需要使用 nibBundle 和 nibName 属性:
- (void)loadView {
if (self.nibBundle && self.nibName) {
[self.nibBundle loadNibNamed:self.nibName owner:self options:nil];
}
}
但这崩溃了:
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
第二个问题是:是否可以在没有任何xib文件的情况下使用故事板加载笔尖?
出于好奇,我刚刚发现self.nibName的值是vXZ-lx-hvc-view-kh9-bI-dsS,其中是故事板#-view-#object 视图中视图控制器的 #object id,视图控制器#中的视图看起来是正确的。
你不能。
loadNibName
应该在您的情况下加载一个名为SOView.xib
的笔尖。
如果您只想加载视图,则可以在单独的xib
文件中创建视图,但是当我们处理情节提要时,我们只在其中添加UIViewController
(当然,可以包含自定义视图(。
要从故事栏实例化方法,请使用此方法:
[self.storyboard instantiateViewControllerWithIdentifier:@"myControllerID"];