gpt4 book ai didi

iphone - 为 UITableViewController 子类调用 loadView

转载 作者:行者123 更新时间:2023-11-28 20:47:32 27 4
gpt4 key购买 nike

我创建了一个没有 xib 的 UITableViewController 子类的实例。我不会使用 xib 文件。我在 -(id)init 例程中构建数据。我在 -(id)init 函数中为表创建数据,并使用 UITableViewDataSource 和 UITableViewDelegate 协议(protocol)的方法来显示和选择数据。我使用 [[UINavigationController alloc] initWithRootViewController: myTVC]; 将 UITableViewController 子类加载到 UINavigationController 中,如果我没有为该类定义 loadView 方法,所有这些都会成功。如果我创建一个空白的 loadView 方法,屏幕上就会显示一个空的 UIView。

我的问题:如何为 UITableViewController 的简单子(monad)类编写正确的 loadView 函数?

最佳答案

根据 Apple 的文档,永远不要调用 [super loadView]: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/loadView

处理这个问题的正确方法是简单地实例化一个 View 并将其设置为 self.view,在本例中也设置为 self.tableView:

- (void)loadView {
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;

self.view = tableView;
self.tableView = tableView;
}

关于iphone - 为 UITableViewController 子类调用 loadView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4370789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com