为什么我得到"无法识别的选择器发送到实例"?

时间:2012-02-10 09:29:18

标签: objective-c ios ios5

我在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>

2 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分:0)

我刚刚在这个练习中遇到了同样的问题一段时间,而且由于真正的答案无处可寻,我会碰到这个问题,以便其他人可以找到它。

除了提到的问题外,从头开始我还有另外两个神秘的问题 每一次原因都证明“公用事业”下的某些价值是不正确的,即使我能够发誓,我也会提前设定它! 如果其他人遇到这种情况,请确保Class(在身份检查器中)和Identifier(在属性检查器中)仍然具有适合您的GUI对象的值,完成输入后所有的代码。

不知道Xcode中是否存在一些意外的行为,它不会保存您的值或稍后无声地还原它们,或者只是因为我对这个环境过于陌生以跟踪我在其中所做的事情。 :)