objective-c - 为什么我得到 "unrecognized selector sent to instance"?

标签 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>

最佳答案

这是因为 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 - 为什么我得到 "unrecognized selector sent to instance"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225539/

相关文章:

iphone - 应用商店 : blocking the game for 3G- devices

ios - 如何在 iOS 中显示来自 IP Camera/CCTV 的 RTSP

cocoa-touch - 可以在 VOIP 应用程序中将多个 tcp 套接字标记为 kCFStreamNetworkServiceTypeVoIP 吗?

iPhone willResignActive 方法未完成执行

ios - AVAsset 无法使用本地音频文件播放

ios - MVC架构中 Controller 的职责

objective-c - 获取输入字符的 Unicode 代码点 - Objective C

objective-c - 使用 CNCopyCurrentNetworkInfo 的问题

ios - 在所选索引处的 tableview 单元格中更改 Imageview 图像

ios - 在 Objective-C 中为 swift 类使用多参数 init 的语法