在任何人将其标记为重复之前,我已经彻底搜索了 Stack Overflow,但没有任何建议有帮助。
问题:我在我的 Master-Detail 应用程序
中添加了一个自定义 View ,当单击 按钮
时,它会被推到主视图之上。我已将 xib
文件的文件所有者设置为自定义类。如果我建立一个 IBOutlet
连接,应用程序崩溃并说“类不是键的键值编码兼容”我已经将 NSLog
放入 我的自定义类的 viewDidLoad
函数,我发现它从未被实例化。我觉得我可能错过了一个非常简单的步骤,但从所有其他关于这个问题的帖子中我可以看出,他们的解决方案对我不起作用。
SettingsViewController.m:
#import "SettingsViewController.h"
@interface SettingsViewController ()
@end
@implementation SettingsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"viewDidLoad");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
启动 View 的代码行:
UIViewController *settingsViewController = [[UIViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[self.navigationController pushViewController:settingsViewController animated:YES];
我已经通过堆栈溢出尝试过的事情:
确保文件的所有者类设置为自定义类名
确保
viewcontroller
中没有任何额外的IBOutlets
在代码中被删除或重命名删除文件并重新创建
清理项目,
kill xcode
,从设备上删除应用程序,重新打开xcode
并重建项目从头开始重建项目
提前感谢您的任何建议!
最佳答案
这一行:
UIViewController *settingsViewController = [[UIViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
应该是:
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
文件所有者类的名称基本上只是 IB 告诉它哪些连接可用的信息。在实例化期间实际上并未使用它。通过调用 [[UIViewController alloc] ...
您正在创建一个普通的 UIViewController
实例(显然没有定义您的键)。
关于ios - XIB 文件使用默认的 ViewController 类而不是文件所有者中设置的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306318/