程序收到EXC_BAD_ACCESS用于InterfaceOrientation

问题描述

||
 - (BOOL)shouldAutorotatetoInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
        Add1.contentStretch=CGRectMake(0.00,0.00,1024.00,66.00);
        background.image = [UIImage imageNamed:@\"back2-landscape.png\"];
    } else  if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
        Add1.contentStretch=CGRectMake(0.00,768.00,66.00);
        background.image = [UIImage imageNamed:@\"back2-portrait.png\"];
    }
    // Return YES for supported orientations
    return YES;
}
在这代码中,我得到了EXC_BAD_ACCESS
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
请告诉我为什么会出现此错误?     

解决方法

鸡肉和鸡蛋问题 您不应该在
shouldAutorotateToInterfaceOrientation:
方法内部访问
interfaceOrientation
属性,因为它会创建一个循环。在不知道为视图控制器启用了哪些接口方向的情况下,视图控制器无法确切地告诉您它的方向是什么(不要将此与设备的方向相混淆),但是在这里您需要在用来确定其方向的相同方法。因此,它创建了一个无限循环,导致崩溃。 您不应该使用这种方法进行布局。看
layoutSubviews
。