开发者

Orientation after if statement

I have this code which calls this method (one that you un comment out to use)

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations

NSLog(@"So the orientation is %d", OrientationInteger); 


//1 is button to the left, 0 to the right.

if (OrientationInteger == 1) {
    return UIInterfaceOrientationLandscapeLeft;
    NSLog(@"setting Orientation landscape left");

}


if (OrientationInteger == 0) {
    return  UIInterfaceOrientationLandscapeRight;
    NSLog(@"setting Orientation landscape right");


}


else {
    return (interfaceOrientation == UIInterfac开发者_如何学编程eOrientationLandscapeRight);
    NSLog(@"no instructions regarding orientation.");


}

}

However, the orientation does not change, nor does the log messages after the first fire.

    NSLog(@"So the orientation is %d", OrientationInteger); 

gives 'So the orientation is 1' or 'So the orientation is 2' sometimes. any ideas?


your issue is that you're returning an int (UIInterfaceOrientation) instead of a BOOL. this method is basically the system asking your view controller if it has permission to rotate. You'll want to return YES for orientations you support, and NO for orientations you don't want to support.

something like

if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    NSLog(@"landscape right allowed");
    return YES;
} else {
    NSLog(@"denied rotation to %i", interfaceOrientation);
    return NO;
}

also as others have pointed out, the return statement exits your method, passing the return value to the method that called it. After return is hit, no more code in your method is run.


The method exits whenever it runs a return statement. So the log messages immediately after return statements will never get displayed.

I don't know why the actual functionality isn't working; it might be a problem somewhere else in the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜