将UIViewController上的类别转换为自定义子类

时间:2012-08-12 16:26:09

标签: objective-c ios uiviewcontroller subclass iboutlet

我有一个类别,为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是IBOutletUITextField。我在viewController中定义的每个IBOutlet都会发生这种情况。为什么要使我的viewController成为我的UIViewController子类的子类,阻止从我的笔尖加载任何内容? nib中的“文件所有者”是ViewController,而不是ATFIPresentationViewController

编辑:嗯,我已经厌倦了尝试让这个工作“适当”,减少打字的方式。我通过将扩展转换为NSObject来实现它,并将UIViewController传递给它。如果有人想看一下,我在gitHub上发布了我使用它的内容。

1 个答案:

答案 0 :(得分:0)

发生此错误是因为您的nib正在尝试将值设置为没有名称basicPicker的属性的类的实例。这是一个可靠的指示,表明您正在向错误类型的对象发送消息。

您需要将nib文件中的File's Owner类设置为您创建的新UIViewController子类,而不仅仅是默认的UIViewController。在您的笔尖中选择File's Owner,然后您可以在Xcode右侧面板中名为 Identity Inspector 的第三个选项卡中更改它,位于顶部。