2011-05-11 46 views
0

Noobie非常耐心。分类? UITabBarController不会自动旋转

我一直在关注O'Rielyy学习iPhone编程和这里的各种线程来构建我的第一个iPhone应用程序。到目前为止好,但在项目结束时的最终绊脚石是越来越应用到自动旋转(测试版仅使用UIWebViews中被否决不自动旋转)

我的邮件应用程序的委托,还增加了一个的UITabBarController

// myNewsUKDelegate.h 
@interface myNewsUKDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { 
    UIWindow *window; 
    UITabBarController *tabBarController; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 
@end 

// myNewsUKDelegate.m 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Add the tab bar controller's view to the window and display. 
    [self.window addSubview:tabBarController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

有用于tabBarController h和.m文件 - 我加入IB所有UINavigationControllers,这反过来又增加一个UITableView

查看图像在http://flatearth.co.uk/nib.png(太小白张贴在问题的图像!)

从我的阅读我明白,问题是我添加到主视图的UITabBarController需要'subclassed'并添加了此代码。

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

下一个视图下/中/子类(无论正确的术语),它的.h和.m文件是它增加了表格视图FirstViewController,这shouldAutorotateToInterfaceOrientation已经设置。

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { 
    UITableView *tableView; 
    NSArray *userList; 
} 
@property (nonatomic, retain) IBOutlet UITableView *tableView; 
@property (nonatomic, retain) NSArray *userList; 
@end 

@implementation FirstViewController 

@synthesize tableView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // I tried adding 
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    // lots of other code ;) 
} 

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

所以,问题似乎是,当[self.window addSubview:tabBarController.view];添加标签栏它不添加shouldAutorotateToInterfaceOrientation返回YES位。

看来我需要添加一个tabBarController子类,其中包含shouldAutorotateToInterfaceOrientation。所以,我读了,并试图此,在interwebs的建议......

// tabBarController.h 
#import <UIKit/UIKit.h> 
@interface tabBarController : UITabBarController { 

} 
@end 

// tabBarController.m 
#import "tabBarController.h" 
@implementation tabBarController 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 
@end 

并添加

#import "tabBarController.h" 

到myNewsUKDelegate.m

但是失败“错误:访问未知“视图”类方法”在myNewsUKDelegate.m

[self.window addSubview:tabBarController.view]; 

线

进一步的搜索没有产生任何帮助,我最近的Xcode知识已经干了:(任何帮助表示赞赏。

回答

8

From my reading I understand that the issue is the UITabBarController I added to the main view needs to be 'subclassed' and have this code added.

不,你不需要那样做。标签栏控制器通过询问所有其子控制器是否支持该方向来确定它是否支持特定的接口方向。在你的情况下,这些似乎是导航控制器,它们反过来询问其当前的子控制器是否支持方向。

换言之,您必须确保所有自定义视图控制器都返回YES以获得所需的界面方向。

+0

Arrrggghhh!你是对的。我错过了*一个*观点。谢谢你的头。 – JulianB 2011-05-11 20:31:29

0

您不需要子类,您需要UITabBarController上的类别。基本上你创建一个名为UITabBarController + Autoresize.h(和。米)

在.H:

#import <UIKit/UIKit.h> 

@interface UITabBarController (Autoresize) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; 
@end 
在.M

#import "UITabBarController + Autoresize.h" 

@implementation UITabBarController (Autoresize) 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    //do custom checks here if you only want to autorotate it in certain views or whatever 
} 
@end 

但由于其他海报指出,所有的视图的父视图要旋转绝支持轮换。

+0

通过重新检查我的代码而尴尬地解决了这个问题,但遇到了类别,这是我需要研究的。感谢您的答案,但这种方法在其他情况下看起来非常方便 – JulianB 2011-05-11 20:34:21

+0

是的,对于像着色导航栏或类似的东西很有用祝你好运! – 2011-05-11 20:35:25