简体   繁体   中英

Use NIB UIView as subview

I'm trying to use a UIView from a XIB to display in a sub view container in another view controller. This is how I try to do display the subView in in MainViewController.h:

@property (nonatomic, strong) UIView *subView;

MainViewController.m:

UIViewController* nameController = [[UIViewController alloc] initWithNibName:@"NamesViewController" bundle:nil];
subView = [nameController view];

[self.view addSubview:subView];

But I get this error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x688e580> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key textField.'

Is there something I am doing wrong or have I missed something? I made a the NamesViewController class with xib, put a textfield in the view, now trying to use the view from this xib as subview.

You should be allocating NamesViewController rather than a "plain" UIViewController :

UIViewController* nameController = [[NamesViewController alloc]
    initWithNibName:@"NamesViewController"
             bundle:nil];

Otherwise, any attempt to connect IBOutlet s would result in a crash. Of course I'm only assuming that the NIB name matches the controller's name; if it does not, you need to use the correct name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM