我有一段调用此方法的代码(您取消注释以使用该方法)
// 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 == UIInterfaceOrientationLandscapeRight);
NSLog(@"no instructions regarding orientation.");
}
}
但是,第一次触发后方向不会改变,日志消息也不会改变。
NSLog(@"So the orientation is %d", OrientationInteger);
有时给出“所以方向是 1”或“所以方向是 2”。 有什么想法吗?
最佳答案
您的问题是您返回的是 int
(UIInterfaceOrientation
) 而不是 BOOL
。
这个方法基本上是系统询问你的 View Controller 是否有旋转的权限。对于您支持的方向,您需要返回 YES
;对于您不希望支持的方向,您需要返回 NO
。
类似的东西
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"landscape right allowed");
return YES;
} else {
NSLog(@"denied rotation to %i", interfaceOrientation);
return NO;
}
也正如其他人指出的那样,return 语句退出您的方法,将返回值传递给调用它的方法。按回车键后,方法中不再运行任何代码。
关于iphone - if 语句后的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4323194/