您的位置:首页 > 移动开发 > IOS开发

iOS 自定义提示框

2015-08-21 17:02 411 查看
在项目的开发中经常需要用到一些提示框,提示用户是否确定进行某项操作。虽然系统提供了一个UIAlter的控件供开发人员使用,但是系统自带往往有局限性,不能满足需求了,很多时候需要自定义提示框。我写了一个比较简单的自定义提示框,当然可以根据自己实际的需求,在我写的基础上面进行自定义,去添加一些更加丰富的功能。

1、新建一个类,命名为:PopAlterView,继承于UIView,然后添加如下代码:

#import <UIKit/UIKit.h>

@interface PopAlterView : UIView

@property (strong, nonatomic) UIView *transparentView;
@property (strong, nonatomic) UIView *alterView;
@property (strong, nonatomic) UILabel *labTitle;
@property (strong, nonatomic) UIButton *btnCancle;
@property (strong, nonatomic) UIButton *btnConfirm;

/**
* 自定义提示框
*
*  @param theView 提示框弹出的view
*  @param title   提示框的标题
*/
- (void)showInView:(UIView *)theView withTitle:(NSString *)title;

//点击按钮后的回调方法
@property (nonatomic,strong) void (^btnAction)(NSInteger tag);

@end


#import "PopAlterView.h"

@implementation PopAlterView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

_transparentView = [[UIView alloc] initWithFrame:frame];
_transparentView.backgroundColor= [UIColor colorWithWhite:0 alpha:0.25];
[self addSubview:_transparentView];

_alterView = [[UIView alloc] initWithFrame:CGRectMake((frame.size.width-200)/2, (frame.size.height-100)/2, 200, 100)];
_alterView.backgroundColor = [UIColor whiteColor];
_alterView.layer.cornerRadius = 5.0;
_alterView.layer.masksToBounds = YES;
[_transparentView addSubview:_alterView];

_labTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 50)];
_labTitle.textAlignment = NSTextAlignmentCenter;
_labTitle.numberOfLines = 0;
[_alterView addSubview:_labTitle];

_btnCancle = [[UIButton alloc] initWithFrame:CGRectMake(10, 60, 80, 25)];
[_btnCancle setTitle:@"取消" forState:UIControlStateNormal];
_btnCancle.layer.cornerRadius = 12.0;
_btnCancle.layer.masksToBounds = YES;
_btnCancle.backgroundColor = [UIColor grayColor];
[_btnCancle setTag:100];
[_btnCancle addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[_alterView addSubview:_btnCancle];

_btnConfirm = [[UIButton alloc] initWithFrame:CGRectMake (105, 60, 80, 25)];
[_btnConfirm setTitle:@"确定" forState:UIControlStateNormal];
_btnConfirm.layer.cornerRadius = 12.0;
_btnConfirm.layer.masksToBounds = YES;
_btnConfirm.backgroundColor = [UIColor blueColor];
[_btnConfirm addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[_btnConfirm setTag:200];
[_alterView addSubview:_btnConfirm];
}
return self;
}

- (void)showInView:(UIView *)theView withTitle:(NSString *)title
{
[theView addSubview:self];
_labTitle.text = title;
}

-(void)btnClick:(id)sender
{
UIButton *btn = (UIButton *)sender;
_btnAction(btn.tag);

[self removeFromSuperview];
}

@end


自定义提示框的类就写好了,然后就可以调用了。

2、在需要调用的地方,进行调用

- (void)btnClick:(id)sender
{
PopAlterView *alterView = [[PopAlterView alloc] initWithFrame:self.view.frame];
[alterView showInView:self.view withTitle:@"测试"];

alterView.btnAction = ^(NSInteger tag){
NSLog(@"%ld",(long)tag);
};
}


对于点击提示框上的“取消”和“确定”按钮需要进行的操作,可以在btnAction这个回调方法中进行。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios