2012-01-13 40 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