【发布时间】: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