【问题标题】:Subclassing UIWindow子类化 UIWindow
【发布时间】:2012-01-13 00:55:42
【问题描述】:

我只是想继承 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

进口

@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 - 对于您的第一条评论,这根本不是一个愚蠢的问题,因为它是正确的。现在我已经在 mainwindow.xib 中将窗口设置为我的子类(并且还在属性中添加了关键字 outlet),当它加载时它会触发我的 NSLog 消息,但是,现在应用程序被冻结了。
  • @mvds - 对于您的第二条评论,我只是喜欢它看起来更好的方式,而且我喜欢有更大的按钮。它与正常高度之间只有 12 像素的差异,所以我认为它不会占用更多空间。

标签: iphone objective-c ios cocoa-touch uiviewcontroller


【解决方案1】:

问题是我包含了 UIWindows - (void)sendEvent:(UIEvent *)event 方法,但没有调用 super 。我打电话给 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 放在常规 UINavigationBar 的顶部。

    【讨论】:

    • 感谢您长期深思熟虑的回复。我改变了这个问题,只是为了让你知道,把它简化为只是试图让 UIWindow 的子类工作。
    • @CoDEFRo 很奇怪,我有一个这样的子类,它可以正常工作。就我而言,我没有覆盖您使用的方法,但这无关紧要。我确实覆盖了-drawRect:-sendEvent:-canBecomeFirstResponder,但我无法想象这会以某种方式产生影响。你确定你没有搞砸别的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多