无法识别的选择器发送到实例

无法识别的选择器发送到实例

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

最佳答案

这是因为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;
        }

关于objective-c - 为什么收到“无法识别的选择器发送到实例”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9225539/

10-09 06:55