0
votes

This is a weird problem..

1.I create a custom “ view ” by xib, and initialize it by

     KWView *oneView = [[[NSBundle mainBundle] loadNibNamed:@"KWView" owner:nil options:nil ]lastObject];

This Xib’s File’s Owner name is “NSObject”(then i try any other more,whatever i choose, it runs smoothly),and there, i choose the view’s Custom Class as “KWView”[This xib named "KWView.xib"]

======= That works !

2.Then i create another custom “viewController” by xib .

 KWMenuVC *menuVC = [[[NSBundle mainBundle] loadNibNamed:@"KWMenuVC" owner:nil options:nil ]lastObject];//  error occurs here
 [self.view addSubview:self.menuVC.view];

This Xib’s File’s Owner name is “KWMenuVC”(i also try NSObject), and there, i choose the Custom Class of this VC’s view as “UIView”.[This xib named "KWMenuVC.xib"]

======= This one can not work, it stucks in “KWMenuVC *menuVC = [[[NSBundle mainBundle]loadNibNamed:@"menuVC" owner:nil options:nil ]lastObject]; “ and the error is “[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view”

3.Later, i try another method to initialize the KWMenuVC ,which works well how matter how i change its Xib’s File’s Owner name

     KWMenuVC *menuVC= [[KWMenuVC  alloc]init]; 
     [self.view addSubview:self.menuVC.view];

======= Works well !

How could this happen?? I used to construct apps' UI by codes. But xib seems more efficient in some cases. Now it seems not a easily understandable stuff.

Thx a lot for your help!

1
this class is not key value coding-compliant for the key view suggests that you have a xib which is attempting to bind some view to the view property of a UIViewController subclass (probably as the File's Owner of that xib). It appears that you are providing a object which is not a UIViewController and does not have a view property so this binding fails. Without more detail about what bindings these xibs contain or which objects are provided as their owners I can't give a more precise answer.Jonah
Bro,thx a lot for your help. I utilized the incorrect method.. initWithNibName:bundle: would be right.Min Wang

1 Answers

0
votes

The correct way to initialize a UIViewController with a XIB is:

KWMenuVC *viewController = [[KWMenuVC alloc]initWithNibName:@"KWMenuVC" bundle:nil];

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle: