2011-05-11 43 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