Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 容器视图控制器中的初始视图控制器未显示_Ios_Uiviewcontroller - Fatal编程技术网

Ios 容器视图控制器中的初始视图控制器未显示

Ios 容器视图控制器中的初始视图控制器未显示,ios,uiviewcontroller,Ios,Uiviewcontroller,我在UIViewController上观看了WWDC视频,并通读了以下博文: 但我无法显示我的初始视图控制器。我有什么遗漏吗?在我的ContainerViewController.m中 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _homeViewController = [[HomeViewController alloc] i

我在UIViewController上观看了WWDC视频,并通读了以下博文:

但我无法显示我的初始视图控制器。我有什么遗漏吗?在我的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的SubViewController属性中确实包含homeViewController和detailViewController。HomeViewController的viewDidLoad也会被调用。我只是在屏幕上看不到任何东西,除了ContainerViewController的背景色


有什么想法吗?谢谢。

您有一个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;
    }
}

所以我不是世界上最聪明的人,但屏幕上什么都不显示的原因是因为笔尖在故事板上,我需要这样做:

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

希望这对还不熟悉情节提要的人有所帮助。

如果通过情节提要在不同于主情节提要的情节提要中设置InitialViewController,则需要更新项目设置以使用新的情节提要

转到“项目设置”、“常规”,并将主界面设置设置为新的情节提要


在iOS 6中,[0]不是问题。我尝试了您的建议,但仍然没有显示HomeViewController。不知道为什么…@Crystal我在最后一行有一个拼写错误。刚刚修好了。您可能希望确保调用了我提供的init方法,而不仅仅是
init()
。我不知道您这样做是否出于教育目的,但这几乎可以通过在故事板中使用容器视图而不使用任何代码来完成。@Crystal在这里发布时,如果您使用的是情节串连板,或者您使用的是ARC,那么这总是很有帮助的。