2011-09-06 77 views
0

假設我在nib文件someNib​​.xib中。爲了讓我能夠告訴我們,如果設備的方向再變,我把這個視圖中沒有加載方法:停止調用由beginGeneratingDeviceOrientationNotifications生成的方法

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

這行做的是,它會調用deviceOrientationChanged方法加班設備旋轉。我的deviceOrientationChanged方法看起來像:

-(void) deviceOrientationChanged{ 

    if(self.view.frame.size.width<800) 
    { 
     //means device is on portrait mode 
    } else { 

     //means device is on landscape mode 
    } 


} 

所以一切工作很好,這個筆尖文件。問題來了,當我加載另一個筆尖文件爲:

  // release allocated variables 
      // VCHome is the name of the nib file 
      VCHome *control = [VCHome alloc]; 
      [control initWithNibName:@"VCHome" bundle:nil]; 
      UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:control]; 
      [self presentModalViewController:navControl animated:YES]; 
      [navControl setNavigationBarHidden:YES]; 
      [control release]; 
      [navControl release]; 

直到這一點,一切正常。我現在在一個新的筆尖文件上,我可以與VCHome進行交互。

爲什麼當我使用VCHome nib文件並旋轉我的設備時,deviceOrientationChanged方法被調用?當我加載VCHome nib文件時,我應該釋放someNib​​(最後一個nib),以便不調用此方法。我不打算再使用一些Nib,並希望我的VCHome筆尖的行爲像我從未加載過一些Nib一樣。

回答

3

的UIViewController有幾種方法,您可以實現處理朝向的變化週期爲您提供:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

如果您使用的是簡單的UIViewControllers這是最好的,而不是用這些方法註冊該事件。

另外,如果你需要檢查設備的方向是橫向或縱向它能夠更好地使用:

UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (UIInterfaceOrientationIsLandscape(currentOrientation)) 
{ 
    ... 
}