2012-09-05 52 views
2

我想在我的項目之一中使用UIViewController遏制功能。該應用僅用於橫向模式。UIViewController遏制

我添加的UIViewController 作爲UIViewController中的子並添加的主視圖爲一個的子視圖的視圖。我也在B中保存對A的引用:

@interface BViewController : UIViewController 

@property (retain, nonatomic) AViewController *aVC; 

@end 

@implementation BViewController : UIViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"]; 
    [self addChildViewController:self.aVC]; 
    [self.myContainerView addSubview:self.aVC.view]; 
} 

@end 

我遇到的問題是橫向不受尊重。我做了一些調試,並找到了解決辦法,但我擔心的是不理想的,因爲它更像是一個黑客:

在B:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"]; 
    [self addChildViewController:self.aVC]; 
    [self.myContainerView addSubview:self.aVC.view]; 
    [self.aVC didMoveToParentViewController:self]; 
} 

在答:

- (void)didMoveToParentViewController:(UIViewController *)parentVC 
{ 
    // Interchange width and height 
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.**height**, self.view.frame.size.**width**); 
} 

我缺少的東西這裏?

回答

6

您的代碼:

self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"]; 
[self addChildViewController:self.aVC]; 
[self.myContainerView addSubview:self.aVC.view]; 

總是錯的。您必須將didMoveToParentViewController:添加到父級控制器後才能發送給子控制器。看到這裏我的討論:

http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

至於旋轉,很可能你只是做這一切爲時尚早。該應用程序始於縱向,並且在調用viewDidLoad時尚未旋轉至橫向。我給的解決方案,在這裏這個問題:

http://www.apeth.com/iOSBook/ch19.html#_rotation

注意的建議那裏,你等到didRotateFromInterfaceOrientation:完成設置您的視圖的外觀。我想你可能在這裏面對我在那裏描述的同樣的問題。

+0

+1鏈接到您的書,非常徹底 –