原文地址:http://blog.sina.com.cn/s/blog_976270ec0100wpnj.html
1. 创建一个继承UIView的视图
//
// DialogView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface DialogView : UIView {
}
-(IBAction)closeClickButton:(id)sender;
@end
//
// DialogView.m
#import "DialogView.h"
@implementation DialogView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]]; //设置视图背景颜色
self.layer.cornerRadius = 10; //设置弹出框为圆角视图
self.layer.masksToBounds = YES;
self.layer.borderWidth = 2; //设置弹出框视图边框宽度
self.layer.borderColor = [[UIColor colorWithRed:0.50 green:0.10 blue:0.10 alpha:0.5] CGColor]; //设置弹出框边框颜色
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0,0, 23, 23)];
[button setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal]; //此按钮用来关闭此视图
[button addTarget:self action:@selector(closeClickButton:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1;
[self addSubview:button];
}
return self;
}
-(IBAction)closeClickButton:(id)sender{
NSLog(@"关闭视图");
self.hidden=YES;
}
- (void)dealloc {
[super dealloc];
}
@end
2. 在UIViewController中的使用
......
//定义弹出框大小,并对弹出框进行操作,可添加控件或者视图。
//弹出框视图可以用来显示图片、文字、表格、视频等等,可以将弹出框视图做成多种形式运用于程序中。
- (IBAction)dialogView:(id)sender {
DialogView *dialogView = [[DialogView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
[self.view addSubview:dialogView];
}