0

我正在使用 iOS5 和情节提要。我有两个具有两个不同标识符的视图。我想在更改设备方向时更改视图。这是我的代码:

- (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;
    }
}
4

2 回答 2

0

UIDeviceOrientationDidChangeNotification释放视图控制器时,您必须删除观察者。

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

于 2012-05-30T12:26:27.357 回答
0
self.view = [self.storyboard instantiateViewControllerWithIdentifier:@"Portrait"];

将返回idaUIViewController并且您将其分配给 a UIView

于 2012-05-30T11:14:48.413 回答