Iphone开发:有没有办法在运行时锁定方向?

joe*_*joe 0 ios4 ios

我需要一个可以在Portrait和landscape中显示的视图.但是当我加载另一个视图时,我只希望它以横向显示.

我有一个视图控制器A.它可以以纵向和横向显示.此视图控制器由App代理创建.

当我单击视图AI上的按钮时,创建一个视图控制器B,我只想在横向上显示.并加载视图,如[A.view addSubView:B.view].

当我旋转设备时,函数shouldAutorotateToInterfaceOrientation仅在
控制器A中调用.无论我如何设置控制器B中的功能.它都无法调用.

And*_*273 5

在第一个视图中,请确保

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    return Yes;
}
Run Code Online (Sandbox Code Playgroud)

在你的第二个视图中设置它

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)

更新:好的,试试这个...
在主视图控制器/应用程序委托中,每次设备旋转时运行shouldAutorotateToInterfaceOrientation,放置一个全局级变量.就像是

.h
@interface NavAppDelegate : NSObject <UIApplicationDelegate> {
    Bool *isMain;
}

.m
-(void)viewDidLoad {
    isMain = Yes;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    if (isMain) {
        return Yes;
    } else {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
}
// This bit is where you'll have to adapt it to how you change your views
// but it's a basic idea.
-(void)bitThatDoesTheViewChange:(viewController *) controller {
   isMain = No;

   *viewController = controller;

   [viewController release];

   isMain = Yes;
}
Run Code Online (Sandbox Code Playgroud)