本AlertView与系统自带的UIAlertView毫无关系,可替代系统的UIAlertView。效果图如下:
代码部分
1、PopupView.h
//
// InitLedView.h
// MagicLights
//
// Created by chendy on 13-5-19.
// Copyright (c) 2013年 chendy. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MyUITextField.h"
@interface PopupView : UIView {
//mac地址输入框
IBOutlet MyUITextField* mac_address_;
//mac密码输入框
IBOutlet MyUITextField* mac_pass_;
//标题
IBOutlet UILabel* label_;
//输入框常态背景
UIImage* textFieldNormalBackground_;
//输入框高亮背景
UIImage* textFieldHilightBackground_;
id delegate;
}
@property(nonatomic,strong)MyUITextField* mac_address;
@property(nonatomic,strong)MyUITextField* mac_pass;
@property(nonatomic,strong)UILabel* label;
@property(nonatomic,strong)UIImage* textFieldNormalBackground;
@property(nonatomic,strong)UIImage* textFieldHilightBackground;
@property(nonatomic)id delegate;
//确认按钮事件
-(IBAction)confirm:(id)sender;
//取消按钮事件
-(IBAction)cancel:(id)sender;
-(IBAction)done:(id)sender;
@end
@protocol PopupViewDelegate <NSObject>
@optional
-(void)PopupView:(PopupView*)aView ButtonClick:(id)sender Index:(int)id;
@end
2、PopupView.m
//
// InitLedView.m
// MagicLights
//
// Created by chendy on 13-5-19.
// Copyright (c) 2013年 chendy. All rights reserved.
//
#import "PopupView.h"
@implementation PopupView
@synthesize mac_address=mac_address_,mac_pass=mac_pass_,label=label_,textFieldHilightBackground=textFieldHilightBackground_,textFieldNormalBackground=textFieldNormalBackground_;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
//点任何位置,键盘消失
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[mac_address_ resignFirstResponder];
[mac_pass_ resignFirstResponder];
}
-(void)confirm:(id)sender {
[delegate PopupView:self ButtonClick:sender Index:0];
}
-(void)cancel:(id)sender {
[delegate PopupView:self ButtonClick:sender Index:0];
}
-(void)done:(id)sender {
[sender resignFirstResponder];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
3、CustomAlertView.h
//
// CustomAlertView.h
// MagicLights
//
// Created by chendy on 13-5-19.
// Copyright (c) 2013年 chendy. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "PopupView.h"
@interface CustomAlertView : UIView<PopupViewDelegate> {
//父视图,用于非模态的,本控件将
UIView* parentView;
//本视图的背景图
UIView* superView;
//内容视图,从nib文件中读取出来
PopupView* contentView_;
//是否是模态的
BOOL isModal;
//代理
id delegate;
}
@property(nonatomic,strong)PopupView* contentView;
@property(nonatomic)id delegate;
@property(nonatomic,strong)UIView* parentView;
@property(nonatomic)BOOL isModal;
//用父视图初始化
-(id)initWithView:(UIView*) aView;
//显示
-(void)show;
//关闭
-(void)close;
@end
@protocol CustomAlerView <NSObject>
@optional
//按钮被按下
-(void)CustomAlertView:(CustomAlertView*)aView ButtonPressed:(id)sender Index:(int)idx;
@end
4、CustomAlertView.m
//
// CustomAlertView.m
// MagicLights
//
// Created by chendy on 13-5-19.
// Copyright (c) 2013年 chendy. All rights reserved.
//
#import "CustomAlertView.h"
@implementation CustomAlertView
@synthesize contentView=contentView_;
@synthesize delegate,parentView,isModal;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];
contentView_ = [nib objectAtIndex:0];
contentView_.delegate = self;
isModal = YES;
}
return self;
}
-(id)init {
self = [super init];
if (self) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];
contentView_ = [nib objectAtIndex:0];
contentView_.delegate = self;
isModal = YES;
}
return self;
}
-(id)initWithView:(UIView *)aView {
self = [super init];
if (self) {
parentView = aView;
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];
contentView_ = [nib objectAtIndex:0];
contentView_.delegate = self;
isModal = YES;
}
return self;
}
-(void)show {
if (isModal) {
contentView_.frame = CGRectMake(contentView_.frame.origin.x, contentView_.frame.origin.y+62, contentView_.frame.size.width, contentView_.frame.size.height);
}
superView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
superView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
[superView addSubview:contentView_];
if (isModal) {
//得到当前的窗口
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview:superView];
} else {
[parentView addSubview:superView];
}
}
-(void)close {
[contentView_ removeFromSuperview];
contentView_ = nil;
[superView removeFromSuperview];
superView = nil;
}
-(void)PopupView:(PopupView *)aView ButtonClick:(id)sender Index:(int)idx {
[self close];
[delegate CustomAlertView:self ButtonPressed:sender Index:idx];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
5、PopupView的nib文件
说明:PopupView.xib文件就是自定义的AlertView中的布局了
使用方法如下:
CustomAlertView* view = [[CustomAlertView alloc] initWithView:self.view];
[view show];
特别说明:目前此方法中的代理事件没添加,控件的事件,PopupView中的delegate传到CustomAlertView,再通过CustomAlertView的delegate传到调至目标位置中。