我有一个类别,为UIViewController
添加了一些方法。我需要在类别中添加一些实例变量,因此我将其转换为自定义UIViewController
子类,并添加了实例变量。然后我将显示的UIViewController
转换为新子类的实例。
现在我在加载应用程序时加载新的UIViewController
时遇到问题。我从AppDelegate
中的笔尖加载视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ATFIPresentationViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ATFIPresentationViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible]; //Crashes Here
return YES;
}
执行此操作后,当我在应用程序窗口调用makeKeyAndVisible
时会抛出异常:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<ATFIPresentationViewController 0x6c497d0> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key basicPicker.'
其中basicPicker是IBOutlet
到UITextField
。我在viewController中定义的每个IBOutlet
都会发生这种情况。为什么要使我的viewController成为我的UIViewController
子类的子类,阻止从我的笔尖加载任何内容? nib中的“文件所有者”是ViewController,而不是ATFIPresentationViewController
。
答案 0 :(得分:0)
发生此错误是因为您的nib正在尝试将值设置为没有名称basicPicker
的属性的类的实例。这是一个可靠的指示,表明您正在向错误类型的对象发送消息。
您需要将nib文件中的File's Owner
类设置为您创建的新UIViewController子类,而不仅仅是默认的UIViewController。在您的笔尖中选择File's Owner
,然后您可以在Xcode右侧面板中名为 Identity Inspector 的第三个选项卡中更改它,位于顶部。