Ios 具有锁定方向的ViewController

Ios 具有锁定方向的ViewController,ios,objective-c,uiviewcontroller,orientation-changes,Ios,Objective C,Uiviewcontroller,Orientation Changes,我对以下情况有疑问: ViewController A应锁定在横向左侧或右侧。VC A的设置如下所示: -(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } -(BOOL)shouldAutorotate { return NO; } -(UIInterfaceOrientationMask)supportedInt

我对以下情况有疑问:

ViewController A应锁定在横向左侧或右侧。VC A的设置如下所示:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}
ViewController B应锁定在纵向上。其设置如下所示:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}
没有导航控制器或选项卡栏。它只是一个普通的视图控制器,然后按一下按钮,我就可以从ViewController a转到ViewController B

[self performSegueWithIdentifier:@"ViewControllerB" sender:self];
到目前为止一切都很好。但当我关闭ViewController B时,ViewController A会显示在纵向上。这就是我如何拒绝VC B的原因:

[self dismissViewControllerAnimated:YES completion:nil];
另一个问题是:当我在ViewController B上并且在纵向上没有问题时,我开始快速地将设备从纵向旋转到横向几次,并且它在某个点旋转到横向,尽管上面有方向规则。是否缺少某些内容?

在ViewDidDisplay中:尝试使用此方法旋转到所需的方向,以防控制器加载时方向错误:

NSNumber *value = @(UIInterfaceOrientationLandscapeRight); // Or whatever else orientation you need.
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
还可以像这样重新定义shouldAutorotate方法:

- (BOOL)shouldAutorotate {
    // Change the YES or NO order depending on your viewController.
    return [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ? YES : NO;
}
并实施此方法:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // Or whatever orientation you need in this viewController.
}

谢谢你的回复。一开始它似乎是有效的——当我去B然后回到A时,它确实加强了A上的风景。但是,当我第二次这样做时,问题再次发生——A在肖像上出现错误。这似乎是一种模式,一次起作用,一次不起作用。我相信有一个解释,但我没有找到它。