另一UIViewController中
内显示的UIViewController我开发一个应用程序,其中UIViewController
(firstViewController)包含一些UILabels
,一个UIButton
和UIView
(子视图)。 UIView
应显示包含一些图层的UIViewController
(secondViewController)。我无法做到这一点。另一UIViewController中
我应该怎么做,以显示secondViewController内子视图firstViewController的?
你可以通过添加另外一个视图控制器作为视角子视图波纹管
SecondVC *aObjSecondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];
[self.view addSubview:aObjSecondVC.view]
可以使用下面的行添加:
[self.subView addSubView:secondViewController.view];
请注意,如果你这样做,你将不会得到'viewWillAppear:'和'viewDidAppear:'事件。一个'UIViewController'是**不是**被设计为像这样添加。我强烈建议不要这种方法。看看MANIAK_dobrii的回答,这在我看来是正确的。 – 2014-08-29 13:14:01
这为什么会有很多票?它不是正确的方式.. – tnylee 2014-08-29 16:58:19
您应该使用的UIViewController遏制或父/子视图控制器。你可以阅读详细信息here。
最基本的版本是:
UIViewController *parentVC = ...
UIViewController *otherVC = ... // it's view will be added as subview
[parentVC addChildViewController:otherVC];
[parentVC.containerView addSubview:otherVC.view]; // containerView is a view where your child view controller should go
[otherVC didMoveToParentViewController:parentVC];
如果只添加其他视图控制器的视图作为一个子视图,子视图控制器将不会接收所有事件。例如,如果您使用这里建议的其他方法(只是将视图添加为子视图,而没有其他),则不会将-viewDidAppear:
消息(和其他消息)发送到您的子视图控制器。
将第二个视图控制器作为childViewController添加到第一个viewController并将第二个视图控制器的视图添加为子视图。 – Akhilrajtr 2014-08-29 11:27:58