简体   繁体   English

iOS:UIViewController不会使用故事板显示另一个UIViewController

[英]iOS: UIViewController doesn't display another UIViewController using storyboards

I am tryin to display multiple UIViewController objects inside a single view. 我正在尝试在单个视图中显示多个UIViewController对象。 For the time being I want to display a single UIViewController object when the app loads. 目前,我想在应用程序加载时显示单个UIViewController对象。 But the app screen appears blank, while it should be displaying a label inside the child view controller. 但是应用程序屏幕显示为空白,而它应该在子视图控制器内部显示标签。

Here is what I did: 这是我所做的:

ParentViewController.h ParentViewController.h

#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
{
    UIViewController *child1Controller;
    UIViewController *child2Controller;
}
@end

ParentViewController.m ParentViewController.m

#import "ParentViewController.h"
#import "Child1Controller.h"
#import "Child2Controller.h"

@interface ParentViewController ()
@end

@implementation ParentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { ... }

- (void)viewDidLoad
{
    child2Controller = [[Child2Controller alloc] init];
    [self.view addSubview:child2Controller.view];

    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)viewDidUnload { ... }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { ... }

@end

Then in the storyboard in interface builder 然后在界面构建器的情节提要中

  • add 3 view controllers 添加3个视图控制器
  • assigned a class to each one of them ParentViewController, Child1Controller & Child2Controller 为他们每个人分配一个类ParentViewController,Child1Controller和Child2Controller
  • in Child2Controller object, added a UILabel inside View. 在Child2Controller对象中,在View内部添加了UILabel。
  • in Child2Controller.h defined the IBOutlet for UILabel and added a synthesize statement for the same in Child2Controller.m 在Child2Controller.h中,为UILabel定义了IBOutlet,并在Child2Controller.m中为其添加了synthesize语句
  • finally in project-Info.plist set the main storyboard file 最后在project-Info.plist中设置主故事板文件

Did I miss something over here? 我在这里错过了什么吗?

Starting from iOS 5 it's possible to take advantage of View Controller Containment. 从iOS 5开始,可以利用View Controller Containment。 This is a new methodology that allows you to create a custom controller container like UINavigationController or UITabBarController . 这是一种新方法,可让您创建自定义控制器容器,例如UINavigationControllerUITabBarController

In your case, this could be very useful. 在您的情况下,这可能非常有用。 In fact, in your storyboard you could create the parent controller and the two child controllers. 实际上,您可以在情节提要中创建父控制器和两个子控制器。 The parent could be linked to another scene while the two children are not linked. 父级可以链接到另一个场景,而两个孩子没有链接。 They are independent scenes that you can use within your parent controller. 它们是独立的场景,您可以在父控制器中使用。

For example in viewDidLoad method of your parent controller you could do the following: 例如,在父控制器的viewDidLoad方法中,您可以执行以下操作:

- (void)viewDidLoad
{
   [super viewDidLoad];

   UIStoryboard *storyboard = [self storyboard];

   FirstChildController *firstChildScene = [storyboard instantiateViewControllerWithIdentifier:@"FirstChildScene"];
   [self addChildViewController:firstChildScene];
   [firstChildScene didMoveToParentViewController:self];
}

Then in your FirstChildController override didMoveToParentViewController 然后在您的FirstChildController重写didMoveToParentViewController

- (void)didMoveToParentViewController:(UIViewController *)parent
{
   // Add the view to the parent view and position it if you want
   [[parent view] addSubview:[self view]];
   CGRect newFrame = CGRectMake(0, 0, 350, 400);
   [[self view] setFrame:newFrame];
}

And voilà! 和瞧! You have a controller that contains one view that is managed by a child controller. 您有一个控制器,其中包含一个由子控制器管理的视图。

For further info see how-does-view-controller-containment-work-in-ios-5 . 有关更多信息,请参见ios-5中的view-viewes-controller-containment-work-in-ios-5

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM