简体   繁体   English

使用NIB UIView作为子视图

[英]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. 我正在尝试使用XIB的UIView来显示在另一个视图控制器的子视图容器中。 This is how I try to do display the subView in in MainViewController.h: 这是我尝试在MainViewController.h中显示subView的方法:

@property (nonatomic, strong) UIView *subView;

MainViewController.m: 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. 我使用xib制作了一个NamesViewController类,在视图中放置了一个文本字段,现在尝试将该xib中的视图用作子视图。

You should be allocating NamesViewController rather than a "plain" UIViewController : 您应该分配NamesViewController而不是“普通” UIViewController

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

Otherwise, any attempt to connect IBOutlet s would result in a crash. 否则,任何尝试连接IBOutlet的尝试都会导致崩溃。 Of course I'm only assuming that the NIB name matches the controller's name; 当然,我只是假设NIB名称与控制器名称相匹配。 if it does not, you need to use the correct name. 如果不是,则需要使用正确的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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