2012-10-17 24 views
1

我一直在嘗試幾種方法來使旋轉按預期工作,但到目前爲止我一直在面對一些問題。在旋轉不正常時切換視圖

現在我想擁有一個包含2個方向視圖的xib文件。一個是肖像,一個是風景。

我在我的主視圖控制器(在應用程序委託中設置根控制器)中嘗試此操作,但不知何故,它失敗了:當我旋轉視圖時,視圖被切換,但插座不旋轉。視圖將顯示爲如果該裝置是在縱向模式的代碼

這裏的比特:

以我的應用程序委託

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
} else { 
    //self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil withOrientation:UIInterfaceOrientationPortrait]; 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 
} 
self.window.rootViewController = self.viewController; 

在Viewcontroller.m

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

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

} 


- (void)orientationChanged:(NSNotification *)notification 
    { 
    [self performSelector:@selector(updateLandscapeView2) withObject:nil afterDelay:0]; 
    } 



    - (void)updateLandscapeView2 
    { 
     UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 
     if (UIDeviceOrientationIsLandscape(deviceOrientation)) 
    { 
    self.view = self.landscapeView; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"landscape" object:nil]; 
    } 
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView) 
{ 
    self.view = self.portraitView; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"portrait" object:nil]; 
    } 
    } 

我打算通過使用本地通知將方向更改轉發到Viewcontroller的子視圖。

無論如何,我在這裏錯過了什麼?我不明白爲什麼這是失敗的。

回答

0

什麼我發現是,當你調用

[UIDevice currentDevice].orientation 

[viewController performSelector .....] 

的上下文中它沒有工作。

一些身體與這些解決方案?