应用程序中无法识别的选择器使用topViewController赋值的didFinishLaunchingWithOptions

时间:2012-05-04 03:25:01

标签: iphone ios

造成这种情况的原因是什么?我在Xcode 4.3上运行iOS 5.1并使用Storyboard。

'NSInvalidArgumentException', reason: '-[WFTopTableViewController topViewController]: unrecognized selector sent to instance 0x6d56f80'


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    // Initial view controller is a Table View Controller, so we look up the window's rootViewController.
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    // Set up the controller.
    WFTopTableViewController *controller = (WFTopTableViewController *)navigationController.topViewController;
    // Pass the controller the managed object context for the first time.
    controller.managedObjectContext = self.managedObjectContext;
    return YES;
}

谢谢! 阿曼达

1 个答案:

答案 0 :(得分:3)

请改为尝试:

(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    WFTopTableViewController *controller = (WFTopTableViewController*)self.window.rootViewController; 
    controller.managedObjectContext = self.managedObjectContext; 
    return YES; 
}

窗口的rootViewController已经是WFTopTableViewController,而不是UINavigationController。如果你想要反过来,请确保在Interface Builder中为UINavigationController选择'Is Initial View Controller'选项。