2012-05-30 89 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上删除观察者。

发生崩溃是因为该通知是在最有可能从内存中删除的类上触发的。