将UIViews作为子视图添加到UIViewController

时间:2014-05-28 17:41:16

标签: ios uiview uiviewcontroller

我用Xib文件创建了2个UIViews。如何将其添加到UIViewController

1 个答案:

答案 0 :(得分:2)

在View Controller中导入firstView并执行以下操作:

firstView *firstView = [[firstView alloc] init];

或者这个:

UIView *firstView = [[[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil] firstObject];

然后,这个:

[self.view addSubview:firstView];

如果您需要访问firstView的子视图,您必须在firstView类中将标记设置为您要访问的子视图:

[subview setTag:100];

然后在View Controller中检索该视图,如下所示:

UIView *subView = [firstView viewWithTag:100];