gpt4 book ai didi

ios - UIViewController 通过 xib 加载另一个 View Controller ,试图访问默认 View 导致崩溃

转载 作者:行者123 更新时间:2023-12-01 19:58:41 24 4
gpt4 key购买 nike

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

'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,而且非常普通/普通..
#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

这是非常无聊和普通的:

enter image description here

这是默认 View - 我尝试为它添加一个 IBOutlet,但它仍然崩溃。

enter image description here

使用自定义类正确设置了 xib View Controller :

enter image description here

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

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

最佳答案

  • 如果您想使用 xib 制作 Controller 的 View ,则不应将 Controller 添加到 InterfaceBuilder,只需添加一个 View 即可。
  • 选择文件的所有者并设置自定义类 (EmbeddedViewController)
  • 通过控制从文件所有者拖动到 View ,将文件所有者的 View 导出连接到新创建的 View ,如图

  • enter image description here

    关于ios - UIViewController 通过 xib 加载另一个 View Controller ,试图访问默认 View 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40977670/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com