2011-07-14 76 views
0

更新:我發現它爲什麼一直加載相同的視圖。我現在修正了這一點。這是一個錯字查看定位變化

ControllerForSplitViews.m

- (id)init 
{ 
    self = [super initWithNibName:@"ControllerForSplitViews" bundle:nil]; 
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){ 
     self.view = landscapeView; 
    } 
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ 
     self.view = portraintView; 
    } 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 
    return self; 
} 

這工作。

現在的問題。當視圖被加載並運行時,我改變方向並從相同的nib文件Fixed加載不同的視圖:但是一旦我這樣做,其他UI元素消失,只有我的橫向視圖的UI元素被保留,當我再次旋轉設備它不再加載我的肖像視圖?任何建議爲什麼會發生這種情況

-(void) orientationChanged:(id)object 
{ 
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     self.view = portraintView; 
    } 
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    { 
     self.view = landscapeView; 
    } 
} 

新問題,每個I改變方向時它加載方向相反的觀點

感謝

回答

0

好了,所以我找到了一個可行的修復,但我不知道爲什麼我的原代碼沒「T工作我所做的是:

刪除:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

刪除:

-(void) orientationChanged:(id)object 
{ 
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     self.view = portraintView; 
    } 
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    { 
     self.view = landscapeView; 
    } 
} 

變化:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){ 

     self.view = landscapeView; 

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
      (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){ 

     self.view = portraintView; 

    } 
    return YES; 
} 

而現在它完美

+0

您的示例使用UIViewController類。那麼,對於所有UIViewControllers,UIViewControllerRotation類別呢?有幾種旋轉方法,例如: - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;' – beryllium