因为在开发的时候 ,所有的 viewcontroller都是继承于同一个VC,导致子类VC上的UIAlertView会截获父类上的UIAlertView的delegate,只好就自己写了一个,因为是父类上的要求比较简单,所以就直接继承了一个UIView,做成效果类似的UIAlertView。其实是没什么难度的。
==================== h ====================
#import
@protocol LNfexibleDelegate
-(void)buttonDownForEvent;
@end
@interface LNFexibleAlertView : UIView
{
NSString *_title;
NSString *_message;
NSString *_btnName;
}
@property (strong,nonatomic) UILabel *titleLabel;
@property (strong,nonatomic) UILabel *messageLabel;
@property (strong,nonatomic) UIButton *btnEvent;
@property (strong,nonatomic) UIImageView *bgView;
@property (strong,nonatomic) UIImageView *centerView;
@property (weak,nonatomic) id
-(id)initWithTitle:(NSString *)title andMessage:(NSString *)message andBtnName:(NSString *)name andFrame:(CGRect)frame;
-(void)dismiss;
@end
============= m ==============
#import "LNFexibleAlertView.h"
@implementation LNFexibleAlertView
@synthesize bgView = _bgView;
@synthesize titleLabel = _titleLabel;
@synthesize messageLabel = _messageLabel;
@synthesize btnEvent = _btnEvent;
@synthesize centerView = _centerView;
-(id)initWithTitle:(NSString *)title andMessage:(NSString *)message andBtnName:(NSString *)name andFrame:(CGRect)frame
{
self = [super init];
if (self) {
_title = title;
_message = message;
_btnName = name;
self.frame = frame;
[self buildStage];
}
return self;
}
-(void)buildStage
{
self.backgroundColor = [UIColor clearColor];
//模糊视图
_bgView = [[UIImageView alloc]initWithFrame:self.frame];
_bgView.backgroundColor = [UIColor blackColor];
_bgView.alpha = 0.52f;
_centerView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, self.frame.size.width-100, 200)];
_centerView.backgroundColor = [UIColor whiteColor];
_centerView.layer.cornerRadius = 8;
_centerView.userInteractionEnabled = YES;
[self addSubview:_bgView];
[self addSubview:_centerView];
//提示信息
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 5, _centerView.frame.size.width-16, 44)];
_titleLabel.font = [UIFont systemFontOfSize:20];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.text = _title;
_messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, _titleLabel.frame.size.height +_titleLabel.frame.origin.y, _titleLabel.frame.size.width, 44)];
_messageLabel.font = [UIFont systemFontOfSize:15];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.numberOfLines = 0;
_messageLabel.textColor = [UIColor blackColor];
_messageLabel.text = _message;
CGSize lableSize = [_message sizeWithFont:_messageLabel.font constrainedToSize:CGSizeMake(_messageLabel.frame.size.width, 200) lineBreakMode:NSLineBreakByTruncatingTail];
CGRect frame = _messageLabel.frame;
frame.size.height = lableSize.height;
_messageLabel.frame = frame;
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, _messageLabel.frame.origin.y+_messageLabel.frame.size.height+10, _centerView.frame.size.width, 1)];
line.backgroundColor = [UIColor lightGrayColor];
[_centerView addSubview:line];
//点击按钮
_btnEvent = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btnEvent.frame = CGRectMake(8, _messageLabel.frame.size.height+_messageLabel.frame.origin.y+17, _titleLabel.frame.size.width, 35);
_btnEvent.titleLabel.textAlignment = NSTextAlignmentCenter;
_btnEvent.titleLabel.font = [UIFont systemFontOfSize:20];
_btnEvent.titleLabel.textColor = [UIColor blueColor];
[_btnEvent setTitle:_btnName forState:UIControlStateNormal];
[_btnEvent addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
[_centerView addSubview:_titleLabel];
[_centerView addSubview:_messageLabel];
[_centerView addSubview:_btnEvent];
CGRect centerNew = _centerView.frame;
centerNew.size.height = _titleLabel.frame.size.height + _messageLabel.frame.size.height + _btnEvent.frame.size.height +33.0f;
_centerView.frame = centerNew;
_centerView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
}
- (void)clickButton
{
[self.LNdelegate buttonDownForEvent];
[self dismiss];
}
-(void)dismiss
{
[self removeFromSuperview];
}
============ 引用 ===============
#define kUIHeight [[UIScreen mainScreen]bounds].size.height
#define kUIWidth [[UIScreen mainScreen]bounds].size.width
/**
* 自定义的 alert
*/
-(void)buildAlert
{
LNFexibleAlertView *alert = [[LNFexibleAlertView alloc]initWithTitle:@"提示" andMessage:@"测试新的alert!" andBtnName:@"确定" andFrame:CGRectMake(0,0,kUIWidth,kUIHeight)];
alert.LNdelegate = self;
[[[[UIApplication sharedApplication]delegate] window] addSubview:alert];
}
-(void)buttonDownForEvent
{
NSLog(@"9099090909090");
}