【问题标题】:Forcing a change in orientation when switching between tabs. (objective c)在选项卡之间切换时强制改变方向。 (目标 c)
【发布时间】:2012-03-28 07:52:36
【问题描述】:

所以我正在测试这个,所以我可以在我更大的项目中使用它。 我有一个名为 TabBar 的 tabbarcontroller 这个 TabBar 有 2 个标签,每个标签都有一个导航控制器。当您单击此按钮时,带有按钮 (OkButtonViewController) 的视图控制器将转到带有标签 (LabelViewController) 的视图控制器。 OkButton 视图控制器始终是纵向的,而 labelViewController 可以切换方向。这仅在出现问题的一种情况下有效。当您在 LabelViewController 中,面向横向并切换选项卡时,OkButtonViewController 也处于横向状态,并保持在横向状态。如何强制 viewcontroll 返回纵向?

这是我的代码。

我可能需要在 TabBar 或 RotatingTabBarAppDelegate 中添加一些内容。我只是不知道是什么。

TabBar.m

  #import "TabBar.h"

  @implementation TabBar

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }

  @end

RotatingTabBarAppDelegate.h

  #import <Foundation/Foundation.h>
  #import "TabBar.h"

  @class RotatingTabBarAppViewController;

  @interface RotatingTabBarAppDelegate : NSObject<UIApplicationDelegate>
  {
      IBOutlet UIWindow *window;
  }

  @property (nonatomic, strong) UIWindow *window;

  @end

RotatingTabBarAppDelegate.m

  @implementation RotatingTabBarAppDelegate

  @synthesize window;

  -(void) applicationDidFinishLaunching:(UIApplication *)application
  {
      UIViewController *tab1 = [[UIViewController alloc] init];
      tab1.tabBarItem =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];

      UIViewController *tab2 = [[UIViewController alloc] init];
      tab2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
      TabBar *tbc = [[TabBar alloc] init];

      [tbc setViewControllers:[NSArray arrayWithObjects:tab1, tab2, nil]];

      [window addSubview:tbc.view];

      [window makeKeyAndVisible];


  }

  @end

OkButtonViewController.h

   #import <UIKit/UIKit.h>

  @interface OkButtonViewContoller : UIViewController


  - (IBAction)ok;

  @end

OkButtonViewController.m

  #import "OkButtonViewController.h"
  #import "LabelViewController.h"

  #define kDetailSegue @"Detail"

  @implementation OkButtonViewContoller


  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      return (interfaceOrientation == UIInterfaceOrientationPortrait);

  }

  - (IBAction)ok 
  {
      [self performSegueWithIdentifier:kDetailSegue sender:@"test"];
  }

  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      if ([segue.identifier isEqualToString:kDetailSegue]) {
          ((LabelViewController *)segue.destinationViewController).testTekst = sender;
      }
  }

  @end

LabelViewController.h

  #import <UIKit/UIKit.h>

  @interface LabelViewController : UIViewController

  @property (nonatomic, strong) NSString *testTekst; 
  @property (nonatomic, strong) IBOutlet UILabel *testLabel;

  @end

LabelViewController.h

  #import "LabelViewController.h"

  @implementation LabelViewController
  @synthesize testTekst;
  @synthesize testLabel;

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      return YES;
  }

  -(void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];
      self.testLabel.text = testTekst;
  }

  @end

抱歉英语不好。

【问题讨论】:

  • 你找到解决这个问题的办法了吗?

标签: objective-c uiinterfaceorientation navigationcontroller tabbarcontroller


【解决方案1】:

您不能强制视图旋转,可能 Apple 会拒绝您的应用,您需要找到另一种设计方式。

【讨论】:

  • 感谢您提供此信息。它记录在某处?
  • 使用公共 API 是不可能的
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多