我的代码编译但是当我尝试启动它时,我遇到了运行时错误。
这是运行时错误:
2011-04-25 23:56:40.689 Noun[39033:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NounAppDelegate 0x4e1c400> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key viewController.'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc75a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1b313 objc_exception_throw + 44
2 CoreFoundation 0x00dc74e1 -[NSException raise] + 17
3 Foundation 0x0002f677 _NSSetUsingKeyValueSetter + 135
4 Foundation 0x0002f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
5 UIKit 0x004b330c -[UIRuntimeOutletConnection connect] + 112
6 CoreFoundation 0x00d3d8cf -[NSArray makeObjectsPerformSelector:] + 239
7 UIKit 0x004b1d23 -[UINib instantiateWithOwner:options:] + 1041
8 UIKit 0x004b3ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
9 UIKit 0x002b917a -[UIApplication _loadMainNibFile] + 172
10 UIKit 0x002b9cf4 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 291
11 UIKit 0x002c4617 -[UIApplication handleEvent:withNewEvent:] + 1533
12 UIKit 0x002bcabf -[UIApplication sendEvent:] + 71
13 UIKit 0x002c1f2e _UIApplicationHandleEvent + 7576
14 GraphicsServices 0x0171f992 PurpleEventCallback + 1550
15 CoreFoundation 0x00da8944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
16 CoreFoundation 0x00d08cf7 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x00d05f83 __CFRunLoopRun + 979
18 CoreFoundation 0x00d05840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00d05761 CFRunLoopRunInMode + 97
20 UIKit 0x002b97d2 -[UIApplication _run] + 623
21 UIKit 0x002c5c93 UIApplicationMain + 1160
22 Noun 0x00001e68 main + 102
23 Noun 0x00001df9 start + 53
)
terminate called after throwing an instance of 'NSException'
这是我的代码(相关方法)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Initiate TabBarController
self.tabBarController = [[UITabBarController alloc] init];
NSMutableArray *viewControllersForTabController = [[NSMutableArray alloc] initWithCapacity:2];
NounViewController *firstViewController = [[NounViewController alloc] initWithNibName:@"NounViewController" bundle:[NSBundle mainBundle]];
[viewControllersForTabController addObject:firstViewController];
[firstViewController release];
PostLoginViewController *secondViewController = [[PostLoginViewController alloc] initWithNibName:@"PostLoginViewController" bundle:[NSBundle mainBundle]];
[viewControllersForTabController addObject:secondViewController];
[secondViewController release];
[tabBarController setViewControllers:viewControllersForTabController];
[viewControllersForTabController release];
[window addSubview:tabBarController.view];
return YES;
}
我确信这可能是我忘记做的微不足道的事情,我正在慢慢加快iOS开发速度
答案 0 :(得分:1)
“NSUnknownKeyException”当您访问不存在的密钥时出现此异常,这并不总是因为代码,Interface Builder中可能存在一些错误的连接。没有问题是上面的代码,我试了一下我的结束,它运行成功。尝试检查您的接口构建器连接可能是您缺少某些东西。最有可能的是你首先尝试通过IB添加tabBarController,然后通过代码来完成它。
答案 1 :(得分:0)
我之前遇到过同样的问题。问题是xib文件中有一个接口构建器连接,在app delegate中没有相应的IBOutlet条目。 这就是我能够解决它的方法: 在MainWindow.xib文件中查找XML标记“IBObjectContainer”。您很可能会找到这样的条目:
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">viewController</string>
<reference key="source" ref="664661524"/>
<reference key="destination" ref="943309135"/>
</object>
<int key="connectionID">11</int>
</object>
您可以安全地删除此条目,因为它似乎已经错误地引入了xib文件。然后,app委托停止期待IBOutlet条目。 或者,您可以通过名称“viewController”在应用程序委托中添加IBOutlet条目。
确保在进行任何更改之前备份xib文件。
可能有一种更简单的方法可以从IB中摆脱这种情况,但我通常不会使用IB,这对我有用。