程序收到了InterfaceOrientation的EXC_BAD_ACCESS

时间:2011-05-26 12:45:30

标签: ipad xcode4 exc-bad-access

 - (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,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))

请告诉我为什么会收到此错误?

1 个答案:

答案 0 :(得分:0)

鸡蛋问题
您不应该从interfaceOrientation方法内部访问shouldAutorotateToInterfaceOrientation:属性,因为它会创建一个循环。在不知道为视图控制器启用了哪些接口方向的情况下,视图控制器无法明确地告诉您它的方向是什么(不要将其与设备的方向区分开来),但是在这里您要使用它用于绘制的相同方法来调用它它的方向。因此它创造了一个导致崩溃的无限循环。

您不应该在此方法中进行布局。看看layoutSubviews