iphone - if 语句后的方向

标签 iphone ios

我有一段调用此方法的代码(您取消注释以使用该方法)

// 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/

相关文章:

带有 Tableview 或 Scrollview 的 iPhone UI?

iphone - 如何在 Obj C 的同一 View 屏幕中删除一个表中的滑动删除按钮而不是另一个表中的滑动删除按钮?

iphone - SQL 的核心数据不起作用

ios - 添加 pod 时终端出错 - [!] 无效的 Podfile 文件 : The target Pods-MyApp already has a platform set

ios - 拇指中心位于轨道起点和终点的 UISlider

ios - 永远不会调用 CAAnimationDelegate 委托(delegate)

ios - 为什么github上的某些ios项目仍然使用IBOutlet而不使用Interface builder文件?

iphone - Facebook API SDK 3.0 "An error occurred with app"

ios - 在选项卡 Controller 中嵌入导航 Controller 会隐藏 View Controller 中的导航项

ios - GPUImage - 尝试在混合过于频繁时过度释放帧缓冲区