iOS — изменение вида с ориентацией устройства

Я использую 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 и поставлю два идентификатора в 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;
    }
}

person Kerberos    schedule 30.05.2012    source источник


Ответы (2)


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

Вернет id в UIViewController, и вы назначаете его UIView.

person Rui Peres    schedule 30.05.2012
comment
Хорошо, если я добавлю два uiview и назначу им два идентификатора, когда я поворачиваю устройство, приложение аварийно завершает работу. Смотри редактуру. - person Kerberos; 30.05.2012
comment
нераспознанный селектор отправлен экземпляру 0x745bbe0 - person Kerberos; 30.05.2012
comment
and then: *** First throw call stack: (0x1aab052 0x1e87d0a 0x1aacced 0x1a11f00 0x1a11ce2 0xa3f7 0x14a5a39 0x1a76885 0x1a767a8 0x13ea1aa 0xcc4df1 0xaff8bd 0xb001f8 0xaf3aa9 0x3d1bfa9 0x1a7f1c5 0x19e4022 0x19e290a 0x19e1db4 0x19e1ccb 0x3d1a879 0x3d1a93e 0xaf1a9b 0x1f18 0x1e75) - person Kerberos; 30.05.2012
comment
Да, шестнадцатеричную часть можно пропустить... Но непризнанный селектор может сказать одну или две вещи о том, что вы делаете. :) В нем должно быть указано имя селектора, который вы отправляете... - person Rui Peres; 30.05.2012

Вы должны удалить наблюдателя на UIDeviceOrientationDidChangeNotification при освобождении контроллера представления.

Сбой происходит из-за того, что это уведомление запускается для класса, который, скорее всего, был удален из памяти.

person Kerni    schedule 30.05.2012