2011-02-06 34 views
0

我花了最近幾個小時試圖讓這個工作,我不能,所以我需要知道,如果我想要做的是正確的事情。它讓我瘋狂!UIViewController和方向是我朝着正確的方向?

我的目標是有一個ViewController檢測方向的變化。當它是縱向時,它顯示一個帶有UITableView的視圖,當它的橫向顯示一個帶有內容的UIView時,我將以編程方式創建它。

在我父視圖 - 控制我有:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

TableDataViewController *tableDataController = [[TableDataViewController alloc] 
      initWithNibName:@"TableDataViewController"                     
        bundle:nil]; 
self.tableDataViewController = tableDataController; 
[self.view insertSubview: tableDataController.view atIndex:0]; 
[tableDataController release]; 
} 

這將加載包含表視圖,並與它去控制我的看法。用戶然後旋轉設備和功能:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toOrientation             
duration:(NSTimeInterval)duration 
{ 
if (toOrientation == UIInterfaceOrientationPortrait) { 
    NSLog(@"PerformAnalysisViewController: Gone to Potrait"); 
} 

if ((toOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (toOrientation == UIInterfaceOrientationLandscapeRight)) { 
    NSLog(@"PerformAnalysisViewController: Gone to Landscape"); 

      // Load new view here 
      CGRect frame = [[UIScreen mainScreen] applicationFrame]; 
     UIView *horizView = [[[HorizView alloc] 
        initWithFrame:frame] autorelease]; 
     [self setView:horizView];; 
} 
} 

將觸發視情況而定。但它不會觸發?這是因爲控制傳遞給我已經插入viewDidLoad的子視圖?如果是這樣,我該如何恢復它?我必須讓它檢測方向,然後從超視圖中刪除它自己嗎?

如果那時工作會按照上面的方式添加新視圖?我曾嘗試閱讀Apple文檔,但無法完成此項工作。

所有幫助非常感謝。

邁克

+0

您是否通過返回YES實現了shouldAutorotateToInterfaceOrientation:interfaceOrientation? – 2011-02-06 23:10:56

回答

0

我使用willRotateToInterfaceOrientation方法,以處理用戶界面旋轉:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { 
    if(interfaceOrientation == UIInterfaceOrientationPortrait || 
     interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     self.backgroundImageView.image = [UIImage imageNamed:@"vertical.png"]; 
    } 
    else { 
     self.backgroundImageView.image = [UIImage imageNamed:@"horizontal.png"]; 
    } 

} 

請檢查一下,你的Info.plist支持多個方向,並且已經實現shouldAutorotateToInterfaceOrientation:interfaceOrientation通過返回YES。

+0

它似乎沒有觸發 - 儘管設置info.pist並在shouldAutorotateToInterfaceOrientation中返回yes:interfaceOrientation – hydev 2011-02-07 20:22:45