2012-02-10 27 views
3

我的第二个应用程序教程apple tut site有问题。在主要课堂上,我得到一个错误。为什么我得到“无法识别的选择器发送到实例”?

的代码:

#import "birdwatchingAppDelegate.h" 

int main(int argc, char *argv[]) { 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([birdwatchingAppDelegate class])); 
    } } 

错误:

[birdwatchingViewController viewControllers]: unrecognized selector 
sent to instance 0x6d37000' 

以下是其中所述错误的位置:

import "birdwatchingAppDelegate.h" 
     #import "BirdSightingDataController.h" 
     #import "birdwatchingViewController.h" 

     @implementation birdwatchingAppDelegate 

     @synthesize window = _window, dataController = _dataController, firstViewController = _firstViewController; 

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     { 
      UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 
      birdwatchingViewController *firstViewController = (birdwatchingViewController *)[[navigationController 
    viewControllers] objectAtIndex:0]; 

      BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init]; 
      firstViewController.dataController = aDataController; 

      return YES; 
     } 

和使问题的确切行是:

birdwatchingViewController *firstViewController = (birdwatchingViewController *)[[navigationController 
    viewControllers] objectAtIndex:0]; 

我找不到问题,有人可以帮忙吗?

感谢..

编辑:

通过添加的NSLog我得到了以下内容:基于评论

2012-02-10 11:24:06.059 Birdwatching[3057:f803] birdwatchingViewController 
2012-02-10 11:24:06.060 Birdwatching[3057:f803] -[birdwatchingViewController viewControllers]: unrecognized selector sent to instance 0x6878250 

编辑:

2012-02-10 11:51:20.696 Birdwatching[3152:f803] navi : <birdwatchingViewController: 0x6a49c20> 
+0

在崩溃之前添加此行 - NSLOG(@“%@”,[[navigationController class] description]); – NeverBe 2012-02-10 09:32:14

+2

请自己和同事们帮忙并遵守命名约定:类名应以大写字母开头,如在“BirdwatchingViewController”和“BirdwatchingAppDelegate”中。所有自定义类型都是如此(例如枚举)。变量和方法应该以小写字符开头。 – DarkDust 2012-02-10 09:55:00

回答

3

这是因为self.window.rootViewController不是UINavigationController它可能很简单UIViewcontroller


编辑基于日志
改变你的方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     { 
      birdwatchingViewController *firstViewController = (birdwatchingViewController *)self.window.rootViewController; 

      BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init]; 
      firstViewController.dataController = aDataController; 

      return YES; 
     } 
+0

嗯...我仍然无法让它工作,同样的错误。 – Roskvist 2012-02-10 10:41:35

+0

打印“navigationController”对象的描述即'NSLog(@“navi:%@”,navigationController);'在'UINavigationController * navigationController =(UINavigationController *)self.window.rootViewController;'之后'并且在你的答案中发布日志。 – 2012-02-10 10:47:27

+0

我已添加日志 – Roskvist 2012-02-10 22:40:16

0

我只是挣扎在这个练习了一段时间同样的问题,由于真正的答案是无处可发现我会碰到这个问题,以便其他人可以找到它。

除了上面提到的问题之外,我从头开始也有另外两个神秘的问题。
每次出现原因都是“Utilities”下的某些值不正确,即使我可以发誓,我也会提前设置!
如果发生这种情况,请确保Class(在身份检查员中)和Identifier(在属性检查器中)的GUI对象仍然具有正确的值,之后您完成输入所有代码。

不知道在Xcode中实际存在一些意外的行为,它不会保存你的值,或者稍后默默地还原它们,或者只是我对这个环境太新以至于无法跟踪我在其中做什么。 :)

相关问题