- (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;
}
In this code I am getting EXC_BAD_ACCESS at the point
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
Please tell me why I am getting this error?
The Chicken and Egg problem
You shouldn’t access the
interfaceOrientationproperty from insideshouldAutorotateToInterfaceOrientation:method as it will create a cycle. Without knowing which interface orientations are enabled for the view controller, the view controller can’t definitively tell you what it’s orientation is (do not mistake this with device’s orientation) but here you are calling for it within the same method which it uses to figure out its orientation. So its creating an infinite loop which leads to a crash.You should not do your layouts in this method. Look at
layoutSubviews.