程序收到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
。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...