Objective c 为什么我会得到;已将无法识别的选择器发送到实例";?
我对上的第二个应用程序教程有问题。在主类中,我得到一个错误 守则:Objective c 为什么我会得到;已将无法识别的选择器发送到实例";?,objective-c,ios,ios5,Objective C,Ios,Ios5,我对上的第二个应用程序教程有问题。在主类中,我得到一个错误 守则: #import "birdwatchingAppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([birdwatchingAppDelegate class])); } } 错误: [bir
#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>
2012-02-10 11:51:20.696观鸟[3152:f803]导航:
这是因为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;
}
在这个练习中,我已经为同一个问题挣扎了一段时间,由于找不到真正的答案,所以我将打断这个问题,以便其他人可以找到它
除了op提到的问题,在从头开始之后,我还有另外两个神秘的问题。
每次都是因为“Utilities”下的某些值不正确,尽管我可以发誓我会更早地设置它
如果其他人发生这种情况,请确保在完成所有代码的输入后,Class
(在Identity inspector中)和Identifier
(在属性检查器中)仍然具有GUI对象的正确值
不知道Xcode中是否确实存在某些意外行为,它不会保存您的值或在以后以静默方式还原它们,或者只是因为我对这个环境太陌生,无法跟踪我在其中所做的事情。:) 在带有崩溃-NSLOG(@“%@,[[navigationController类]说明]的行之前添加此行;请您自己和同事帮个忙,遵守命名约定:类名应以大写字符开头,如BirdwatchingViewController
和BirdwatchingAppDelegate
中所述。所有自定义类型(如枚举)也是如此。变量和方法应以小写字符开头。打印navigationController
对象的描述,即NSLog(@“navi:%@”,navigationController)代码>在UINavigationController*导航控制器=(UINavigationController*)self.window.rootViewController之后
并将日志发布到您的答案中。