如何将具有特定ID的故事板加载到UIViewController?

时间:2013-06-04 11:59:49

标签: uiviewcontroller storyboard

以下是我的代码:

UIViewController *viewController = [UIViewController new];
if (indexPath.section == 0){
        UIViewController *controller;
        switch (indexPath.row) {
            case 0:
                controller = [[self storyboard] instantiateViewControllerWithIdentifier:@"ProfileSettings"];
                [[viewController view] setBackgroundColor:[UIColor greenColor]];

我想要的只是将控制器“加载”到viewController中,但我不确定它是否可能..你们中任何人都可以解释我做错了吗?

1 个答案:

答案 0 :(得分:0)

我不确定为什么你需要先创建一个新的视图控制器,然后实例化另一个视图控制器并将其放在你最初创建的视图控制器中。

做这样的事情会更好:

id viewController;
if (indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            viewController = [[self storyboard] instantiateViewControllerWithIdentifer:@"ProfileSettings"];
    // etc etc

// Display view controller as required

...但是,如果你真正想要做的是将实例化的故事板视图控制器放在另一个视图控制器的之内,那么你将不得不使用视图控制器包含,这是可用的从iOS 5开始。视图控制器包含的工作方式如下:

[vc addChildViewController:instantiatedController];
[vc.view addSubview:instantiatedController.view];

但我不确定你是否真的需要这样做。它从您发布的代码看起来就像没有真正需要使用包容。