将 Interface Builder 中的自定义视图添加到另一个视图?

问题描述 投票:0回答:1

我在 Interface Builder 中有一个自定义 UIViewController (在我的故事板中),我想添加这个以编程方式组装到另一个视图中的视图。我已经为我制作的视图控制器创建了一个类,但简单地导入并添加该自定义类作为子视图似乎不起作用。

iphone objective-c ipad uiviewcontroller storyboard
1个回答
2
投票

您需要从 UIStoryboard 对象加载视图控制器的实例,并将其视图添加为子视图。该代码看起来像这样:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController* myVc = [storyboard instantiateViewControllerWithIdentifier:@"ident"];
[self.view addSubview:myVc.view];

确保在 IB 中设置视图控制器的标识符字段并将其传递给“instantiateViewControllerWithIdentifier”方法。

© www.soinside.com 2019 - 2024. All rights reserved.