[英]How to subview a UITableViewController within another Controller using Storyboards
I have encapsulated all my table view logic on a UITableViewController that is linked to a view. 我已将所有表视图逻辑封装在链接到视图的UITableViewController上。 This was done using storyboards.
这是使用故事板完成的。
I would like to embed this logic and view within another view controller / view (kind of like a header information with a scrollable table beneath.) 我想在另一个视图控制器/视图中嵌入这个逻辑和视图(有点像下面带有可滚动表的标题信息。)
I have the following components: CustomViewController which is linked to a UIView (dragged in from storyboard) CustomTableViewController which is linked to a UITableView (dragged in from storyboard) 我有以下组件:CustomViewController链接到UIView(从故事板中拖入)CustomTableViewController链接到UITableView(从故事板中拖入)
Essentially I am trying to mimic the scenario of the Stopwatch in the iOS clock app 基本上我试图模仿iOS时钟应用程序中秒表的场景
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks
谢谢
Ok figured it out. 好想通了。 This solution is iOS 5 specific since this feature was added there.
此解决方案是iOS 5特定的,因为此处添加了此功能。 This method works with storyboards.
此方法适用于故事板。
Setup: The intention is to host one view controllers view and logic within another controller. 设置:目的是在另一个控制器中托管一个视图控制器视图和逻辑。
Since there is no intrinsic way to reference the child view controller in the storyboard, we need to give the view controller a name. 由于没有内在的方法来引用故事板中的子视图控制器,我们需要为视图控制器命名。 This can be done by filling out the "Identifier" attribute on the controller in the storyboard.
这可以通过填充故事板中控制器上的“标识符”属性来完成。 NOTE: Make sure you are giving the controller the identifier and not the controllers view.
注意:确保为控制器提供标识符而不是控制器视图。
Instantiate the controller you want to aggregate. 实例化要聚合的控制器。 This can be done from the hosting controller.
这可以从托管控制器完成。
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ControllerIdentifier"];
Add the child controller to the parent controller 将子控制器添加到父控制器
[self addChildViewController: controller];
Add the child controllers view to the parent controllers view. 将子控制器视图添加到父控制器视图。 Note if you have a place holder view in the parent controller you wish to add the child view to, then this is where you do it.
请注意,如果您希望将子视图添加到父控制器中的占位符视图,则可以在此处执行此操作。 Here I add it the a UIView called stage in the parent controller.
在这里,我在父控制器中添加一个名为stage的UIView。
[self clearStage]; [self.stageView addSubview:controller.view]; presentedController.view.frame = self.stageView.bounds;
And that is it. 就是这样。 Pretty simple.
很简单。 I have used it successfully with switching controllers and view in a home made tab control.
我已成功使用它与切换控制器和在自制选项卡控件中查看。 The sub controller enlists its views in the view lifecycle, so the
viewDidLoad
, etc all work as expected in this child view controller. 子控制器在视图生命周期中查看其视图,因此
viewDidLoad
等都在此子视图控制器中按预期工作。
Hopes this helps someone. 希望这有助于某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.