2012-03-03 144 views
2

我試圖做到這一點:在我家的應用程序中,如果我將設備置於橫向模式,我將顯示幫助視圖。到目前爲止,它似乎可以正常工作,但如果我按下按鈕轉到另一個視圖,那麼我會回到主屏幕並將設備置於橫向模式中......我以一種錯誤的方式看到了所有內容。根據設備方向切換視圖

這裏有兩個圖像,試圖解釋這個問題:

wrong landscape wrong portrait

這裏是我ViewController.m代碼:

#import "ViewController.h" 

@implementation ViewController 
@synthesize portraitView, landscapeView; 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

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


// Do any additional setup after loading the view, typically from a nib. 
} 


-(void)orientationChanged:(NSNotification *)object{ 
UIDeviceOrientation deviceOrientation = [[object object] orientation]; 

    if (deviceOrientation == UIInterfaceOrientationPortrait) 
{ 
    self.view = self.portraitView;   
} 
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==  
UIInterfaceOrientationLandscapeRight) 
{ 
    self.view = self.landscapeView;  
} 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
[super viewDidDisappear:animated]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation: 
(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
} 
} 

@end 

我希望你能幫助我

+0

如果您可能向我發送您的代碼,我會修復它並將其發回。 – tipycalFlow 2012-03-07 08:25:03

+0

當然!所以也許你可以幫我解決一些其他小問題。如何通過電子郵件? – Enzoses 2012-03-07 09:10:38

+0

不,沒有人...只是這一個...你也得自己弄清楚事情...... :) – tipycalFlow 2012-03-07 10:22:36

回答

0

您可以在viewDidAppear中對當前的方向進行額外的檢查並更改accordi的視圖ngly。加入此方法類搜索:你總是可以得到設備的當前定向

[[UIDevice currentDevice] orientation]; 

編輯

-(void)viewDidAppear{ 
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

if (deviceOrientation == UIInterfaceOrientationPortrait) 
{ 
    self.view = self.portraitView;   
} 
else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==  
UIInterfaceOrientationLandscapeRight) 
{ 
self.view = self.landscapeView;  
} 
} 

EDIT2-嘗試手動設置方向爲:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

    [[UIDevice currentDevice] setOrientation:deviceOrientation]; 
    if (deviceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view = self.portraitView;   
    } 
    else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation ==  
UIInterfaceOrientationLandscapeRight) 
    { 
     self.view = self.landscapeView;  
    } 
}