2010-12-01 53 views
0

我有这样的代码调用该方法(一个,你未注释掉使用)方向后if语句

// 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'。 有什么想法?

回答

0

只要它运行return语句,方法就会退出。所以在return语句后面的日志消息将永远不会显示。

我不知道为什么实际的功能不工作;这可能是代码中其他地方的问题。

1

你的问题是你要退回intUIInterfaceOrientation)而不是BOOL。 这个方法基本上是要求你的视图控制器是否有权限旋转的系统。您需要为您支持的方向返回YES,对于您不想支持的方向,请返回NO

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

也正如其他人指出的那样,return语句退出你的方法,该返回值传递给调用它的方法。返回命中后,您的方法中不会再运行代码。