iOS 自定义提示框
来源:互联网 发布:python twisted 编辑:程序博客网 时间:2024/05/16 12:21
在项目的开发中经常需要用到一些提示框,提示用户是否确定进行某项操作。虽然系统提供了一个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这个回调方法中进行。
0 0
- iOS 自定义提示框
- iOS--自定义提示框
- iOS自定义活动提示框
- iOS自定义alertView提示框
- ios自定义提示框,不影响程序进程
- IOS开发-07.自定义HUD(提示框)
- iOS弹出一个View(自定义的提示框)
- iOS 用UIView自定义版本升级提示框
- js自定义提示框
- 弹出自定义提示框
- 自定义提示框
- 自定义AlertDialog提示框
- Android自定义提示框
- 自定义提示框
- 自定义alert提示框
- 自定义提示框
- 自定义弹出提示框
- bootstrap自定义提示框
- 寻找有环链表的环路开头结点
- 黑马程序员——009——JavaAPI①(String类、StringBuffer、StringBuilder、基)
- (基于jQuery的)图片上传之前本地预览
- powerdesigner显示注释
- ios系统能力&api转载链接汇总
- iOS 自定义提示框
- 项目功能
- C#中抽象类与接口的区别
- 【leetcode】House Robber I && II
- iOS 企业版app发布问题
- Java图片验证码的实现
- 线程6:互斥量,解决线程冲突
- 编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。
- Orchard编写网上商店模块2