UIViewController 通过 xib 加载另一个视图控制器,试图访问默认视图导致崩溃

UIViewController loading another view controller via xib, trying to access the default view causes crash

所以,我有这个问题,这应该是微不足道的。我有一个视图控制器正在从 xib 加载另一个视图控制器。尝试引用该辅助视图控制器的视图时崩溃:

'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "Embedded" nib but the view outlet was not set.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010689134b __exceptionPreprocess + 171

执行此操作的代码片段如下所示:

- (void)viewDidLoad {
     [super viewDidLoad];

     // loads controller just fine.

     EbeddedViewController *embedded = [[EbeddedViewController alloc] initWithNibName:@"Embedded" bundle:nil];

     // KABOOM on line below 
     UIView *embeddedViewIs = embedded.view;

}

EmbeddedViewController 扩展了 UIViewController,非常ordinary/plain..

#import "EbeddedViewController.h"

@interface EbeddedViewController ()

@property (nonatomic, strong) IBOutlet UILabel *embeddedLabel;

@end

@implementation EbeddedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这很无聊也很普通:

这是默认视图 - 我尝试为其添加 IBOutlet,但它崩溃了 none-the-less.

xib 视图控制器已正确设置自定义 class:

我不知道为什么会崩溃。如果您想试用这个小应用程序,请在此处下载:

Link to a tiny two view controller app where this crash is demonstrated

  1. 如果你想用xib制作controller的view,你不应该在InterfaceBuilder中添加controller,只添加view。
  2. Select 文件所有者并设置自定义 class (EmbeddedViewController)
  3. 通过控制从文件所有者拖动到图像上看到的视图,将文件所有者的视图出口连接到新创建的视图