#import <UIKit/UIKit.h>
@interface CustomWindow :UIWindow {
UIView *superView;
UIView *backgroundView;
UIImageView *backgroundImage;
UIView *contentView;
BOOL closed;
}
@property (nonatomic,retain)UIView *superView;
@property (nonatomic,retain)UIView *backgroundView;
@property (nonatomic,retain)UIImageView *backgroundImage;
@property (nonatomic,retain)UIView *contentView;
-(CustomWindow *)initWithView:(UIView *)aView;
-(void)show;
-(void)close;
@end
#import "CustomWindow.h"
@implementation CustomWindow
@synthesize superView;
@synthesize backgroundView;
@synthesize backgroundImage;
@synthesize contentView;
-(UIImage *) pngWithPath:(NSString *)path
{
NSString *fileLocation = [[NSBundlemainBundle]pathForResource:pathofType:@"png"];
NSData *imageData = [NSDatadataWithContentsOfFile:fileLocation];
UIImage *img=[UIImageimageWithData:imageData];
return img;
}
-(CustomWindow *)initWithView:(UIView *)aView
{
if (self=[superinit]) {
//内容view
self.contentView = aView;
//初始化主屏幕
[selfsetFrame:[[UIScreenmainScreen]bounds]];
self.windowLevel =UIWindowLevelStatusBar;
self.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.1];
//添加根view,并且将背景设为透明.
UIView *rv = [[UIViewalloc]initWithFrame:[selfbounds]];
self.superView = rv;
[superViewsetAlpha:0.0f];
[self addSubview:superView];
[rv release];
//设置background view.
CGFloat offset = -6.0f;
UIView *bv = [[UIViewalloc]initWithFrame:CGRectInset(CGRectMake(0,0,self.contentView.bounds.size.width,self.contentView.bounds.size.height), offset, offset)];
self.backgroundView = bv;
[bv release];
//用圆角png图片设为弹出窗口背景.
UIImageView *bi = [[UIImageViewalloc]initWithImage:[[selfpngWithPath:@"alert_window_bg"]stretchableImageWithLeftCapWidth:13.0topCapHeight:9.0]];
self.backgroundImage = bi;
[backgroundImagesetFrame:[backgroundViewbounds]];
[backgroundViewinsertSubview:backgroundImageatIndex:0];
[backgroundViewsetCenter:CGPointMake(superView.bounds.size.width/2,superView.bounds.size.height/2)];
[superViewaddSubview:backgroundView];
CGRect frame =CGRectInset([backgroundViewbounds], -1 * offset, -1 * offset);
//显示内容view
[backgroundViewaddSubview:self.contentView];
[self.contentViewsetFrame:frame];
closed =NO;
}
returnself;
}
//显示弹出窗口
-(void)show
{
[selfmakeKeyAndVisible];
[superView setAlpha:1.0f];
}
-(void)dialogIsRemoved
{
closed = YES;
[contentViewremoveFromSuperview];
contentView =nil;
[backgroundViewremoveFromSuperview];
backgroundView =nil;
[superViewremoveFromSuperview];
superView =nil;
[self setAlpha:0.0f];
[selfremoveFromSuperview];
self = nil;
// NSLog(@"===> %s, %s, %d", __FUNCTION__, __FILE__, __LINE__);
}
-(void)close
{
[UIViewsetAnimationDidStopSelector:@selector(dialogIsRemoved)];
[superView setAlpha:0.0f];
// NSLog(@"===> %s, %s, %d", __FUNCTION__, __FILE__, __LINE__);
}
接下来,在CustomAlertWindowViewController中添加该自定义CustomWindow对象成员,并添加一个button属性与XIB界面上的Press Me!按钮相连接,如下图:- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[buttonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(id)sender {
NSLog (@"+++ doAction executing. +++");
NSArray *nib = [[NSBundlemainBundle]loadNibNamed:@"ContentView"owner:selfoptions:nil];
UIView *tmpContentView = [nibobjectAtIndex:0];
UIButton *tmpButton = (UIButton *)[tmpContentViewviewWithTag:2];
[tmpButton addTarget:selfaction:@selector(okAction:)forControlEvents:UIControlEventTouchUpInside];
customWindow = [[CustomWindowalloc]initWithView:tmpContentView]; //将刚加载进来的xib中的view作为参数传递给CustomWindow的contentView。
[customWindowshow];
}
- (void)okAction:(id)sender{
NSLog (@"+++ okAction executing. +++");
[customWindowclose];
customWindow.hidden =true;
}