且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ios中添加视图控制器作为子视图

更新时间:2023-12-05 17:20:34

添加自定义 UIView 主视图中的对象(在XIB中),您要在其中添加并显示子视图控制器。让 contentView 是该视图的名称。使用以下代码添加子视图控制器:

Add a custom UIView object in your main view (in XIB) in which you want to add and show your child view controller. Let contentView is the name of that view. Use following code to add child view controller:

self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self addChildViewController:self.loginView];
[self.loginView.view setFrame:CGRectMake(0.0f, 0.0f, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[self.contentView addSubview:self.loginView.view];
[self.loginView didMoveToParentViewController:self]; 

如果你不添加最后一行,你的子视图控制器将不会收到事件。通过使用此代码,您可以同时在父视图控制器和子视图控制器中接收事件。

if you don't add last line, your child view controller will not receive events. By using this code you can simultaneously receive events in both parent and child view controllers.