2013-12-21 8 views
0

我使用下面的代碼來實現它,但它不起作用。請給我一些建議。非常感謝。在iOS中定位時加載另一個.xib

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

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

} 

orientationChanged()函數:

- (void)orientationChanged:(NSNotification *)notification{ 
     [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; 
    } 

adjustViewsForOrientation()函數:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation { 

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"load portrait view"); 
     [[NSBundle mainBundle] loadNibNamed:@"DashBoardViewController" owner:self options:nil]; 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
    { 
     NSLog(@"load landscapeview"); 
     [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil]; 
    } 
} 

運行時總是顯示DashBoardViewController.xib。在lanscape中,它仍然顯示DashBoardViewController.xib。我不知道爲什麼。

回答

1

這是因爲你沒有實際設置視圖。

你需要做的是這樣的:

self.view = [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil] objectAtIndex:0]; 

這將加載從廈門國際銀行文件的視圖,並設置當前視圖到該視圖。

不要忘記像在代碼中看到的那樣添加objectAtIndex:0。

+0

非常感謝,它工作正常:) – user3125692

+0

真棒,很高興我可以幫助! – adeiji

0

使用willRotateToInterfaceOrientation:方法,而不是通知

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
    { 
     NSLog(@"load landscapeview"); 
     [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil]; 
     //[self viewDidLoad]; 
    } 
    else 
    { 
     NSLog(@"load portrait view"); 
     [[NSBundle mainBundle] loadNibNamed:@"DashBoardViewController" owner:self options:nil]; 
     //[self viewDidLoad]; 
    } 
} 

也許你不得不uncoment [self viewDidLoad],如果您有索姆邏輯存在。

+0

謝謝,我試過了,但它仍然是一樣的。它不工作:( – user3125692

+0

@ user3125692你試着用viewDidLoad? – Greg

+0

@Greg'viewDidLoad'不安全查詢方向 –