我看了UIViewController的遏制了WWDC的視頻,並通過這個博客帖子閱讀:http://www.cocoanetics.com/2012/04/containing-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的背景顏色,我只是沒有看到屏幕上的任何內容。
有什麼想法?謝謝。
的[0]是不是在iOS的6個問題我想你建議,但它仍然不顯示HomeViewController。不知道爲什麼仍然... – Crystal
@Crystal我在最後一行有一個錯字。只是修復它。你可能想要確保我提供的init方法被調用,而不僅僅是'init()'。 –