5

我在故事板文件中设置了一个容器视图,但我想在设置一些属性后以编程方式嵌入视图控制器。这是我在父 VC 的 viewDidLoad 中的代码:

_workoutVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"entryTableVC"];
_workoutView = _workoutVC.tableView;

[self addChildViewController:_workoutVC];

[_workoutVC.tableView setFrame:_container.bounds];
[_container addSubview:_workoutView];

但是,在任何时候都不会调用 child 中的 viewDidLoad。当我运行模拟器时,我的容器视图是空白的。我在调试器中检查了我的所有属性都不是nil.

4

7 回答 7

0

If you aren't using an embed segue and are adding child view controllers manually, why use a container view at all? Try this:

_workoutVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"entryTableVC"];
_workoutView = _workoutVC.tableView;

[self addChildViewController:_workoutVC];

[_workoutVC.view setFrame:self.view.frame];
[self.view addSubview:_workoutVC.view];
于 2013-07-29T23:00:35.220 回答
0

第一次访问该属性时调用该viewDidLoad:方法。view我假设在你的情况下view== tableView。如果没有,你必须调整loadView你的孩子VC的方法。

- (void)loadView {
    //create your table view here
    //...
    self.view = self.tableView;
}

然后只需使用viewproperty 而不是tableView.

_workoutView = _workoutVC.view;

编辑:

完整的代码是:

_workoutVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"entryTableVC"];
_workoutView = _workoutVC.view;

[self addChildViewController:_workoutVC];

[_workoutView setFrame:_container.bounds];
[_container addSubview:_workoutView];
于 2013-07-29T22:26:45.760 回答
0

我也有类似的情况。我在容器视图控制器中使用了普通的 UIView(与 Xcode 提供的“容器视图”相反)。

我使用的事件顺序是:

    ChildViewController *childViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"];
    childViewController.view.frame = self.viewForChildViewController.frame;
    self.viewForChildViewController = childViewController.view;
    [self.view addSubview:self.viewForChildViewController];

    [self addChildViewController:childViewController];
    [childViewController didMoveToParentViewController:self];

这有点像跳舞,但它允许我以编程方式设置子视图控制器,并确保在子视图控制器上调用 -viewDidLoad。此外,您应该能够在此之后设置一些属性。

于 2013-08-05T19:20:06.543 回答
0

抱歉,这已经晚了,但我刚刚遇到了这个问题。这是 InterfaceBuilder 的那些晦涩问题之一,有时它不会为自定义类创建完整的信息,因此 segue 没有找到控制器类。

在我的例子中,我有两个相同的带有嵌入式 TableViewControllers 的视图。第一个有效,第二个没有。在将 Storyboard 作为源代码检查时,我发现第二个缺少一些额外的信息,当我添加它时,调用了 viewDidLoad。要解决这个问题

1)将您的故事板作为源代码打开。

2) 搜索为您的嵌入式视图控制器找到 customClass

customClass="MyEmbeddedViewController"

2)在其后添加以下值(MyAppProject 应该是您的项目的名称,目标只是目标)。

customModule="MyAppProject" customModuleProvider="target"

您的嵌入式控制器现在应该触发 viewDidLoad 并正常工作。

于 2017-02-24T17:14:05.283 回答
0

(Swift 2)由于这个问题没有公认的答案......

我最终做的是在子视图控制器上创建一个方便的 init:

convenience init() {
    self.init(nibName: "ChildViewController", bundle: nil)
    //initializing the view Controller form specified NIB file
}

在 parentViewController 的viewDidLoad()中:

let commentsView = CommentsViewController()
self.addChildViewController(commentsView)
self.momentsScrollView.addSubview(commentsView.view)
commentsView.didMoveToParentViewController(self)

我使用了一个简单的 UIView 而不是 UIContainerView,但它是相似的。

于 2016-01-29T23:00:44.853 回答
-1

“我想在设置一些属性后以编程方式嵌入视图控制器”---你不能这样做。如果你有一个容器视图,你会自动获得一个嵌入其中的视图控制器,并且它的 viewDidLoad 在父控制器的 viewDidLoad 之前被调用。如果要设置嵌入式控制器的某些属性,可以在父控制器的 prepareForSegue 中进行。

于 2013-07-29T22:35:41.230 回答
-3

对于任何可能仍然有同样问题的人。

我有类似的情况,只需调用 loadView() 即可解决。

let sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let controller = sb.instantiateViewControllerWithIdentifier("LoadingView") as! LoadingViewController
controller.loadView()

然后我可以访问视图中的所有内容。

于 2016-03-18T13:58:21.863 回答