子类化UIWindow

5
我只是想子类化UIWindow,以便我可以拦截一些通知。除了下面列出的代码之外,我还需要进入MainWindow.xib并将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

import

@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;
}

也许是一个愚蠢的问题,但是你在xib中设置了主窗口的类吗? - mvds
但是你为什么要这样做呢?如果你设置了正确的自动调整大小掩码,那么你的视图将与变化的导航栏完美对齐。它在横向模式下缩小一点是有合理的原因的。 - mvds
@mvds - 对于你的第一个评论,这并不是一个愚蠢的问题,因为它是正确的。现在我已经将窗口设置为我的主窗口.xib中的子类(并且还将关键字outlet添加到属性中),当它加载时,它会触发我的NSLog消息,但是现在应用程序已经冻结了。 - Ser Pounce
@mvds - 对于你的第二条评论,我只是喜欢它看起来更好,而且我喜欢有更大的按钮。正常高度只相差12像素,所以我认为它不会占用太多空间。 - Ser Pounce
2个回答

4
问题出在我包含了UIWindows的-(void)sendEvent:(UIEvent *)event方法,但是没有调用它的super方法。我调用了它的super方法之后问题得到解决。

2

编辑:请注意,这是在整个问题被重写之前回答原始问题的。

首先,您应该找出是否有一种记录的方式(某种调用或覆盖方法)来更改导航栏的高度。我非常怀疑是否有这样的方法。我也怀疑UIWindow是寻找它的地方 - 导航栏是UINavigationController的一部分。

假设没有合法的方法可以强制高度保持为44像素,您可以尝试以下几种方法:

  1. (不推荐)破坏UINavigationController的整个自动旋转机制,自己处理视图控制器的旋转,例如从UIWindow中开始处理,就像您现在正在做的那样。 UINavigationController只会“感觉到”调整大小,并不知道它实际上正在旋转。UINavigationController不能进行旋转,因此-shouldAutoRotate.. { return NO; }。作为一个快速检查,看看如果您只是将顶层VC的框架设置为CGRectMake(0,0,480,320);(无论是从您的子类化UINavigationController还是从子类化的UIWindow),会发生什么。如果一切正常工作,那么您只需要添加自动旋转部分。
  2. (推荐)不要依赖UINavigationController来绘制导航栏,而是自己完成所有操作。这样做可能不太有趣,但99%的保证您将使其正常工作,并且它不会在每次iOS更新时出现故障。
  3. (中立、快速而肮脏的修复)对于横向模式,请在常规导航栏上方放置自己的UINavigationBar。

感谢您的长篇回复,思路清晰。我想让您知道,我已经修改了问题,将其简化为仅尝试使UIWindow的子类正常工作。 - Ser Pounce
@CoDEFRo 奇怪,我有一个完全相同的子类,它可以正常工作。在我的情况下,我没有覆盖你所覆盖的方法,但这应该没有关系。我确实覆盖了 -drawRect:-sendEvent:-canBecomeFirstResponder 方法,但我无法想象这会有任何影响。你确定没有搞砸其他什么吗? - mvds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接