2012-01-13 73 views
4

我只是試圖子類UIWindow,以便我可以攔截一些通知。隨着下面列出的代碼,我也進入MainWindow.xib並更新UIWindow對象到我的子類。它加載正常,問題是我的標籤欄上的選項卡沒有響應(在下面的例子中,我只添加了一個選項卡,但在我的應用程序中我有多個(這不是問題))。任何人都可以看到我可能做錯了什麼?謝謝。子類化UIWindow

UISubclassedWindow.h

#import <UIKit/UIKit.h> 

@interface UISubclassedWindow : UIWindow 
{ 

} 

@end 

UISubclassedWindow.m

#import "UISubclassedWindow.h" 

    @implementation UISubclassedWindow 

- (id) initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     NSLog(@"init"); 
    } 
    return self; 
} 

    - (void)makeKeyAndVisible 
    { 
     [super makeKeyAndVisible]; 
     NSLog(@"makeKeyAndVisible"); 
    } 

    - (void)becomeKeyWindow 
    { 
     [super becomeKeyWindow]; 
     NSLog(@"becomeKeyWindow"); 

    } 

    - (void)makeKeyWindow 
    { 
     [super makeKeyWindow]; 
     NSLog(@"makekeyWindow"); 
    } 

    - (void)sendEvent:(UIEvent *)event 
    { 
    } 

    - (void)dealloc 
    { 
     [super dealloc]; 
    } 

    @end 

AppDelegate.h

進口

@class UISubclassedWindow;

@interface My_AppAppDelegate : NSObject <UIApplicationDelegate> 
{ 
    UISubclassedWindow *window; 
} 

@property (nonatomic, retain) IBOutlet UISubclassedWindow *window; 

@end 

AppDelegate.m

@synthesize window; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UITabBarController *tabBarController = [[UITabBarController alloc] init]; 

    MainViewController *mainViewController = [[MainViewController alloc] initWithViewType: 0]; 
    UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController]; 
    mainNavigationController.title = @"Main"; 
    [[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack]; 

    [tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]]; 

    [self.window setRootViewController: tabBarController]; 
    [self.window makeKeyAndVisible]; 

    [mainViewController release]; 
    [mainNavigationController release]; 
    [tabBarController release]; 

    return YES; 
} 
+0

也許是個愚蠢的問題,但是你在xib中設置了主窗口的類嗎? – mvds 2012-01-13 01:10:50

+0

但你爲什麼要這樣?如果你放置正確的自動識別模板,那麼你的視圖將會與不斷變化的導航欄完美對齊。有一個合理的原因是它在景觀上略微縮小。 – mvds 2012-01-13 01:14:02

+0

@mvds - 對你的第一個評論,根本不是一個愚蠢的問題,因爲它是正確的。現在我已經將窗口設置爲mainwindow.xib中的子類(並且還將關鍵字outlet添加到了屬性中),並且當它加載它時會激發我的NSLog消息,但現在應用程序被凍結。 – 2012-01-13 01:18:50

回答

4

問題是我包括UIWindows - (void)sendEvent:(UIEvent *)事件方法,但沒有調用super。我稱它爲超級,一切都是固定的。

2

編輯:注意,這回答了原來的問題,重大改寫改變整個發行前

你應該首先找出是否有是一種文檔化的方式(某些調用或重寫方法)來更改導航欄的高度。我非常懷疑有一個。我也懷疑UIWindow是否是尋找它的地方 - 導航欄是UINavigationController的一部分。

假設有給力的高度,以保持44px沒有合法的方式,你可以嘗試幾件事情:

  1. (不推薦)打破的UINavigationController的整個自轉機械,處理視圖控制器自己的旋轉,例如來自UIWindow,就像你現在開始做的那樣。 UINavigationController只會「感覺」一個調整大小,並不知道它實際上正在旋轉。 UINavigationController一定不能旋轉,所以-shouldAutoRotate.. { return NO; }。作爲一個快速檢查,看看會發生什麼,如果你只是將最頂層的VC的幀設置爲CGRectMake(0,0,480,320);(來自你的子類UINavigationController或從子類UIWindow)。如果一切仍然正常,那麼你只需要添加自動旋轉部分。
  2. (推薦)不要依賴UINavigationController來繪製導航欄,而是自己做。沒有多少樂趣可做,但99%的保證你會得到它的工作,它不會在iOS的每一次更新中破壞。
  3. (中性,快速&髒修復)景觀模式,把你自己的UINavigationBar頂部的常規之一。
+0

感謝您長期以來深思熟慮的迴應。我改變了這個問題,只是爲了讓你知道,簡化它只是試圖讓UIWindow的子類工作。 – 2012-01-13 09:11:49

+0

@CoDEFRo很奇怪,我有這樣的子類,它只是起作用。在我的情況下,我沒有覆蓋你的方法,但這並不重要。我重寫'-drawRect:','-sendEvent:','-canBecomeFirstResponder',但我無法想象這會以某種方式有所作爲。你確定你沒有搞亂別的嗎? – mvds 2012-01-13 13:32:33