0

我在 UIViewController Containment 上观看了 WWDC 视频并通读了这篇博文:http ://www.cocoanetics.com/2012/04/ contains-viewcontrollers/

但我无法显示我的初始视图控制器。有什么我想念的吗?在我的 ContainerViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _homeViewController = [[HomeViewController alloc] init];
    _detailViewController = [[DetailViewController alloc] init];

    [self setSubViewControllers:@[_homeViewController, _detailViewController]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (_selectedViewController.parentViewController == self) {
        // nothing to do
        return;
    }

    // adjust the frame to fit the container view
    _selectedViewController.view.frame = _containerView.bounds;

    // make sure that it resizes on rotation automatically
    _selectedViewController.view.autoresizingMask = _containerView.autoresizingMask;

    // add as child VC
    [self addChildViewController:_selectedViewController];

    // add it to container view, calls willMoveToParentViewController for us
    [_containerView addSubview:_selectedViewController.view];

    // notify that move is done
    [_selectedViewController didMoveToParentViewController:self];
}

- (void)loadView {
    // set up the base view
    CGRect frame = [[UIScreen mainScreen] bounds];
    UIView *aView = [[UIView alloc] initWithFrame:frame];
    aView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    aView.backgroundColor = [UIColor blueColor];

    // set up content view
    _containerView = [[UIView alloc] initWithFrame:frame];
    _containerView.backgroundColor = [UIColor grayColor];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [aView addSubview:_containerView];

    self.view = aView;
}

- (void)setSubViewControllers:(NSArray *)subViewControllers {
    _subViewControllers = [subViewControllers copy];

    if (_selectedViewController) {
        // remove previous VC
    }

    _selectedViewController = _subViewControllers[0];

}

我的 ContainerViewController 是我故事板中的初始视图控制器。我看到它显示在模拟器上,但 HomeViewController(我的容器中的第一个子视图控制器)没有显示。

当我单步调试调试器时,我的 ContainerViewController 的 subViewControllers 属性中确实包含 homeViewController 和 detailViewController。HomeViewController 的 viewDidLoad 也会被调用。我只是在屏幕上看不到任何东西,除了 ContainerViewController 的背景颜色。

有什么想法吗?谢谢。

4

3 回答 3

0

你有一个 NSArray,但你试图将它作为一个 C 数组来访问。

_subViewControllers[0]

应该:

[_subViewControllers objectAtIndex:0];

话虽如此,您似乎有一些代码在其他方法中可能会更好。我会亲自清理很多,让它变得更简单。我会删除 loadView 和 _containerView,然后像往常一样使用 self.view。对于您正在尝试做的事情,似乎甚至不需要跟踪父视图控制器和子视图控制器。无论如何,这就是我的做法:

@interface ContainerViewController ()
@property (nonatomic, retain) NSArray *subViewControllers;
@end

@implementation ObservationReportViewController {
    UIViewController *_selectedViewController;
}
@synthesize subViewControllers = _subViewControllers;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        HomeViewController *homeViewController = [[HomeViewController alloc] init];
        homeViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        DetailViewController *detailViewController = [[DetailViewController alloc] init];
        detailViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        // Retain the view controllers.
        self.subViewControllers = @[homeViewController, detailViewController];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setSelectedViewController: [_subViewControllers objectAtIndex:0]];
}

-(void)setSelectedViewController:(UIViewController *)selectedViewController {
    if (_selectedViewController != selectedViewController) {
        [_selectedViewController.view removeFromSuperview];
        _selectedViewController = selectedViewController;
        // adjust the frame to fit the container view
        [self.view addSubview:_selectedViewController.view];
        //_selectedViewController.view.frame = _containerView.bounds;
        _selectedViewController.view.frame = self.view.bounds;
    }
}
于 2012-12-05T05:59:37.313 回答
0

所以我不是世界上最聪明的人,但屏幕上什么都没有显示的原因是因为笔尖在情节提要中,我需要这样做:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
    _homeViewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
    _detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

希望这对还不熟悉 Storyboard 的人有所帮助。

于 2012-12-05T19:00:06.407 回答
0

如果您通过与 MainStoryboard 不同的故事板中的故事板设置 InitialViewController,则需要更新项目设置以使用该新故事板。

在此处输入图像描述

转到项目设置,常规并将主界面设置设置为新故事板

在此处输入图像描述

于 2016-12-13T19:47:24.793 回答