所以我測試這個,所以我可以用它在我的大項目上。 我有一個名爲TabBar的tabbarcontroller 此TabBar有2個選項卡每個選項卡都有一個navigationcontroller。一個帶按鈕的視圖控制器(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
遺憾的英語不好。
你有沒有找到解決這個問題的辦法? – k06a 2012-06-16 16:35:55