2012-05-30 87 views
0

我正在使用iOS5和故事板。我有兩個不同的標識符視圖。 我想在更改設備的方向時更改視圖。 這是我的代碼:iOS - 用設備方向更改視圖

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
     (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){ 

     self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Landscape"]; 

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

     self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Portrait"]; 

    } 

    return YES; 
} 

但是應用程序崩潰。 這是什麼問題?

編輯: 如果我添加兩個uiview,我把這兩個id到uiview,當我旋轉設備它崩潰。 這是代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

} 

- (void) orientationChanged:(id)object 
{ 
    portraitView = [self.storyboard instantiateViewControllerWithIdentifier:@"TermoregolatoreTop"]; 
    landscapeView = [self.storyboard instantiateViewControllerWithIdentifier:@"Landscape"]; 

    UIInterfaceOrientation interfaceOrientation = [[object object] orientation]; 

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

     self.view = self.portraitView; 
    } 
    else 
    { 

     self.view = self.landscapeView; 
    } 
} 

回答

0
self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Portrait"]; 

將返回idUIViewController且將其分配給一個UIView

+0

好的,如果我添加兩個uiview,並且我將這兩個id分配給這些時,我旋轉設備的應用程序崩潰。 看看編輯。 – Kerberos

+0

崩潰說什麼? – Peres

+0

無法識別的選擇發送到實例0x745bbe0 – Kerberos

0

釋放視圖控制器時,您必須在UIDeviceOrientationDidChangeNotification上刪除觀察者。

發生崩潰是因爲該通知是在最有可能從內存中刪除的類上觸發的。