今天,在xcode5上想试下如何新建一个*.xib和UIviewControl结合起来,所以新建了一个空的iPhone项目。然后新建了一个xib文件和一个继承自UIViewControl的类。
在Appledelegate.m文件中添加如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//添加的代码
myTestViewController *myTest = [[myTestViewController alloc] initWithNibName:@"myTestView" bundle:nil];
self.window.rootViewController = myTest;
//[myTest release];//开启了ARC,会自动释放内容
return YES;
}
按正确的方法进行文件关联,然后编译运行出现如下错误:
014-04-02 10:32:53.264 myTest01[751:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x8a58320> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
*** First throw call stack:
(
0 CoreFoundation 0x017345e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b78b6 objc_exception_throw + 44
2 CoreFoundation 0x017c46a1 -[NSException raise] + 17
3 Foundation 0x01178c2e -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
4 Foundation 0x010e4f3b _NSSetUsingKeyValueSetter + 88
5 Foundation 0x010e4493 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
6 Foundation 0x0114694a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
7 UIKit 0x004cbcd5 -[UIRuntimeOutletConnection connect] + 106
8 libobjc.A.dylib 0x014c97d2 -[NSObject performSelector:] + 62
9 CoreFoundation 0x0172fb6a -[NSArray makeObjectsPerformSelector:] + 314
10 UIKit 0x004ca82e -[UINib instantiateWithOwner:options:] + 1417
11 UIKit 0x004cc5bb -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 165
12 UIKit 0x00229fcb -[UIApplication _loadMainNibFileNamed:bundle:] + 58
13 UIKit 0x0022a2f9 -[UIApplication _loadMainInterfaceFile] + 245
14 UIKit 0x00228e9f -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 543
15 UIKit 0x0023d42c -[UIApplication handleEvent:withNewEvent:] + 3447
16 UIKit 0x0023d999 -[UIApplication sendEvent:] + 85
17 UIKit 0x0022ac35 _UIApplicationHandleEvent + 736
18 GraphicsServices 0x036d72eb _PurpleEventCallback + 776
19 GraphicsServices 0x036d6df6 PurpleEventCallback + 46
20 CoreFoundation 0x016afdd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
21 CoreFoundation 0x016afb0b __CFRunLoopDoSource1 + 523
22 CoreFoundation 0x016da7ec __CFRunLoopRun + 2156
23 CoreFoundation 0x016d9b33 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x016d994b CFRunLoopRunInMode + 123
25 UIKit 0x002286ed -[UIApplication _run] + 840
26 UIKit 0x0022a94b UIApplicationMain + 1225
27 myTest01 0x0000305d main + 141
28 libdyld.dylib 0x01d72701 start + 1
29 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
网上有很多解决这个问题的方案,尝试了很多都没用。让后发现在*.plist文件里面有个Main nib file base name,把这个去掉就可以了