makeKeyWindowAndVisible无效

时间:2011-12-31 09:19:19

标签: ios uiwindow

我创建了一个新的UIWindow并希望在窗口中显示一个像popover视图一样的视图,但是它没有显示出来。(代码我读SVProgressHUD作为参考)

代码如下:

在.h文件中

#import <UIKit/UIKit.h>

@interface PopoverView : UIWindow

@property (nonatomic, strong) NSArray *items;

- (void)show;

@end

在.m文件中

@interface PopoverView () 

@property (nonatomic, assign) UIWindow *prevKeyWindow;

@end


@implementation PopoverView

@synthesize items=_items;
@synthesize prevKeyWindow;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.windowLevel = UIWindowLevelAlert;
        // Initialization code
        UILabel *view = [[UILabel alloc] initWithFrame:frame];
        view.backgroundColor = [UIColor blackColor];
        [self addSubview:view];
        self.backgroundColor = [UIColor blackColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return self;
}

- (void)show {
    if (![self isKeyWindow]) {
        self.prevKeyWindow = [UIApplication sharedApplication].keyWindow;
        [self makeKeyAndVisible];
    }
}

@end

有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

PopoverView变量必须是全局变量,如果它是局部变量,UIWindow将被销毁,因此您无法看到它(出现并且很快消失)。