如何以编程方式为故事板中的子视图设置视图控制器? [英] how to set view controller programmatically for subview in storyboard?

查看:29
本文介绍了如何以编程方式为故事板中的子视图设置视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在故事板中设计,截图如下)我的 rootviewcontroller 视图中有两个子视图

在我的代码中,我想为每个子视图分配一个单独的视图控制器.即为 TableView 分配一个 tableViewController.

我尝试在awakeFromNib(或ViewDidLoad)方法中执行此操作,但无济于事.我的 tableview 控制器中的委托方法永远不会被调用.我认为甚至在我分配的 tableviewcontroller 可以执行某些操作之前,storyboard 就可以在此处加载子视图.

self.myTableViewController = (TodoListViewController *)[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];self.myTableView.delegate = self.myTableViewController;self.myTableView.dataSource = self.myTableViewController;self.myTableViewController.tableView = self.myTableView;

我不确定在故事板中有这样的视图时是否允许这样做,或者我做错了什么?

解决方案

我来到这个网站是因为我遇到了类似的问题.实际上,我正在做完全相同的事情:我有一个带有两个子视图的视图控制器(全部定义在具有很多约束的故事板中).

在 viewDidLoad 的 containerviewcontroller 中,我正在执行与您相同的调用(但我首先定义了视图):

self.myTableViewController = (TodoListViewController *)[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];self.myTableViewController.tableView = self.myTableView;self.myTableView.delegate = self.myTableViewController;self.myTableView.dataSource = self.myTableViewController;

这对我有用(我想,这是您的问题:您周围没有导航控制器....并确保您的 tableview 的出口在故事板中真正连接.).

但真正的问题出现在正确接线之后.如果你进入一个单元格,你可能想给 nextView 一些 cellID.由于您手动定义了 tableviewcontroller,因此只会调用您的委托方法 didSelectRowAtIndexPath,而不会调用 prepareForSegue.

我已经尝试从故事板实例化视图控制器

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"aStoryboard" bundle:nil];self.myTableViewController = [storyboard instantiateViewControllerWithIdentifier:@"myTableViewID"];

但是segue没有被调用.直接调用它 (performSegueWithIdentifier:...) 不起作用,因为这个 tableviewcontroller 不是由导航控制器管理的.

对我来说,结果是在 containerViewController 中调用了 prepareForSegue 方法,该方法可以将调用委托给其子视图控制器.

实际上真正的解决方案是不要. 有一个容器视图"对象可以添加到视图并定义视图控制器的一个区域,该区域可以包含一个子视图控制器.这才是我真正想做的.

(Designed in storyboard , screenshot below) I have two subviews on my rootviewcontroller's view

In my code i want to assign a separate view controller to each subview. i.e Assign a tableViewController to the TableView.

I tried to do this in awakeFromNib (or ViewDidLoad) method but to no avail. The delegate method in my tableview controller are never called. I think storyboard does the job of loading the subviews here even before the tableviewcontroller i assign can do something.

self.myTableViewController = (TodoListViewController *)[[UITableViewController alloc]  initWithStyle:UITableViewStylePlain];
self.myTableView.delegate = self.myTableViewController;
self.myTableView.dataSource = self.myTableViewController;
self.myTableViewController.tableView = self.myTableView;

I am not sure if this is allowed when having views like this in storyboard or i am doing anything wrong ?

解决方案

I came to this site as I had a similar problem. Actually I am doing the exact same thing: I have a viewcontroller with two subviews (all defined in a storyboard with lots of constraints).

in the containerviewcontroller in viewDidLoad I am doing the same calls as you do (but I defined the view first):

self.myTableViewController = (TodoListViewController *)[[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
self.myTableViewController.tableView = self.myTableView;
self.myTableView.delegate = self.myTableViewController;
self.myTableView.dataSource = self.myTableViewController;

That works for me (I guess, here is your problem: You don't have a navigation controller around it.... and be sure that the outlets of your tableview are really connected in the storyboard.).

But the real problem comes after the correct wiring. If you tab into a cell, you probably want to give some cellID to the nextView. As you have defined your tableviewcontroller manually, only your delegate method didSelectRowAtIndexPath gets called, but not the prepareForSegue.

I have played around with instantiating the viewcontroller from the storyboard

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"aStoryboard" bundle:nil];
self.myTableViewController = [storyboard instantiateViewControllerWithIdentifier:@"myTableViewID"];

but the segue did not get called. calling it directly (performSegueWithIdentifier:...) did not work as this tableviewcontroller is not managed by a navigation controller.

For me it ended up that the prepareForSegue method gets called in the containerViewController which can delegate the calls to its subviewcontrollers.

EDIT: Actually the real solution is DON'T. There is a "Container View" object which can be added to a view and defines a region of a view controller that can include a child view controller. This is what I really wanted to do.

这篇关于如何以编程方式为故事板中的子视图设置视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆