我需要从现有 View 中以编程方式显示 UITableViewController。
我有一个 MyAppViewController,它的 View 上的按钮应该打开一个新的 UITableViewController。但到目前为止,我似乎无法让它工作。这是 UITableViewController 的样子:
@interface SecondViewController : UITableViewController{}
在主视图上,我使用以下命令添加 TableView Controller :
SecondViewController vc2 = [[SecondViewController alloc] init];
vc2.view.frame = {...}
[self.view addSubview: vc2.view];
这就是我通常添加一个简单 View Controller 的方式,所以我认为 UITableViewController 是一样的。
我得到的结果是整个屏幕上的空单元格。这是因为未设置委托(delegate),我假设,但我会在哪里设置呢?所以我的困惑是 UITableViewController View Controller 上的委托(delegate) setter 并从主视图 Controller 的 View 正确显示 UITableViewController 。
最佳答案
编辑
这个答案已经过时了。将 View Controller 的 View 添加为另一个 View Controller 的 subview 的正确方法是实现容器 View Controller 。
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
原始答案
你的模式有点不同寻常。执行您所描述的操作的最常见方法是初始化并呈现 SecondViewController。如果这确实是您想要的,我强烈建议您仔细阅读 Apple 的文档,了解如何创建、自定义和呈现 UIViewController
和 UITableViewController
.
如果您知道所有这些并且真的想做一些自定义的事情,那么请继续阅读。
相反,您正在创建 SecondViewController 的实例,而不是呈现它,而是将其 View 添加到当前 View Controller View 中。如果你知道你想要完成什么,而这实际上是你想要的结果,那就去做吧。
配置这种通用模式的方法不止一种。在此示例中,我将坚持使用最简单的方法。
1) MainViewController
(MVC) 应该保留 SecondViewController
(SVC) 实例中的一个属性,只要它是需要的。
2) SVC 是 UITableViewController
的子类所以默认设置为dataSource
和 delegate
因为它是 UITableView
.这意味着您需要实现 UITableViewDataSource
和 UITableViewDelegate
SVC
中的方法为您的表填充数据。假设 MVC
知道哪些数据需要进入表中,它应该将其传递给 SVC
.最简单的方法是在 SVC
上定义一个属性可以在 MVC
中设置在初始化时。
3) 假设有一种方法可以在表格呈现后将其关闭,您将需要 MVC
要做到这一点。基本上,MVC
将删除 SVC
的 View 从它的 superview 然后设置 SVC
属性(property)为零。
这是一些快速的伪代码。我写了最低限度的例子。
// MainViewController.h
//
#import "SecondViewController.h"
@interface MainViewController : UIViewController
@property (nonatomic, strong) SecondViewController *svc;
@end
// MainViewController.m
//
#import "MainViewController.h"
@implementation MainViewController
// init and configure views w/ init, loadView, viewDidLoad, etc
// present SecondViewController
- (void)presentSecondViewController:(id)sender {
self.svc = [[SecondViewController alloc] init];
// this example uses an array as the SVC data
self.svc.tableData = @[@"first", @"second", @"third", @"fourth"];
self.svc.view.frame = self.view.bounds;
[self.view addSubview:self.svc.view];
}
// dismiss SecondViewController
- (void)dismissSecondViewController:(id)sender {
if (self.svc) {
[self.svc.view removeFromSuperview];
self.svc = nil;
}
}
// SecondViewController.h
//
@interface SecondViewController : UITableViewController
@property (nonatomic, strong) NSArray *tableData;
@end
// SecondViewController.m
//
@implementation SecondViewController
// init and configure views w/ init, loadView, viewDidLoad, etc
// override tableData getter to create empty array if nil
- (NSArray *)tableData
{
if (!tableData) {
_tableData = @[];
}
return _tableData;
}
// override tableData setter to reload tableView
- (void)setTableData:(NSArray *)tableData
{
_tableData = tableData;
[self.tableView reloadData];
}
// implement UITableViewDelegate and UITableViewDataSource methods using
// the self.tableData array
关于iphone - 以编程方式将 UITableViewController 添加到当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420528/