所以我正在测试这个,所以我可以在我更大的项目中使用它。我有一个名为 TabBar 的 tabbarcontroller 这个 TabBar 有 2 个标签,每个标签都有一个导航控制器。当您单击此按钮时,带有按钮 (OkButtonViewController) 的视图控制器将转到带有标签 (LabelViewController) 的视图控制器。OkButton 视图控制器始终是纵向的,而 labelViewController 可以切换方向。这仅在出现问题的一种情况下有效。当您在 LabelViewController 中,面向横向并切换选项卡时,OkButtonViewController 也处于横向状态,并保持在横向状态。我怎样才能强制viewcontroll回到纵向?
这是我的代码。
我可能需要在 TabBar 或 RotatingTabBarAppDelegate 中添加一些内容。我只是不知道是什么。
标签栏.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
标签视图控制器.h
#import <UIKit/UIKit.h>
@interface LabelViewController : UIViewController
@property (nonatomic, strong) NSString *testTekst;
@property (nonatomic, strong) IBOutlet UILabel *testLabel;
@end
标签视图控制器.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
抱歉英语不好。