iOS开发-轻松学会封装自定义视图view(自定义弹框封装详解)
来源:互联网 发布:青少年行知实践园的题 编辑:程序博客网 时间:2024/06/05 06:01
原文地址:点击打开链接
我们在做开发时经常会自定义一些view,并且如果如果该view比较复杂或者经常被调用到,我们就可以将该view单独放到一个类中,方便调用或者修改。笔者前段时间封装了一个自定义的弹出框,功能简单,借此跟大家交流一下封装自定义的视图view的方法与心得,仅仅是为了分享学习,并不建议直接在项目中使用,如果有错误或者不足之处,欢迎大家前来批评指正。
先上效果图:
在ViewController.m中 导入"SZKAlterView.h"头文件
调用代码:
SZKAlterView *lll=[SZKAlterView alterViewWithTitle:@"简书号:iOS_凯" content:@"感谢各位朋友的关注与鼓励" cancel:@"取消" sure:@"确定" cancelBtClcik:^{ //取消按钮点击事件 NSLog(@"取消"); } sureBtClcik:^{ //确定按钮点击事件 NSLog(@"确定"); }]; [self.view addSubview:lll];
需要注意的是:
SZKAlterView直接封装成了类方法,并且可以手动填写标题,内容,取消按钮的内容,确定按钮的内容,还有一个需要注意的就是笔者将取消按钮和确定按钮的点击事件利用block传递出来了,具体实现方法,下面讲到。
实现方法:
SZKAlterView.h 中
#import <UIKit/UIKit.h>//取消按钮点击事件typedef void(^cancelBlock)();//确定按钮点击事件typedef void(^sureBlock)();@interface SZKAlterView : UIView@property(nonatomic,copy)cancelBlock cancel_block;@property(nonatomic,copy)sureBlock sure_block;/** * 简书号:iOS_凯 http://www.jianshu.com/users/86b0ddc92021/latest_articles * * @param title 标题 * @param content 内容 * @param cancel 取消按钮内容 * @param sure 确定按钮内容 * @param cancelBlock 取消按钮点击事件 * @param sureBlock 确定按钮点击事件 * * @return SZKAlterView */+(instancetype)alterViewWithTitle:(NSString *)title content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure cancelBtClcik:(cancelBlock)cancelBlock sureBtClcik:(sureBlock)sureBlock;@end
SZKAlterView.m中
-(instancetype)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { //标题 _titleLb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 50)]; _titleLb.textAlignment=NSTextAlignmentCenter; _titleLb.textColor=[UIColor blackColor]; [self addSubview:_titleLb]; //内容 _contentLb=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLb.frame), self.bounds.size.width, 50)]; _contentLb.textAlignment=NSTextAlignmentCenter; _contentLb.textColor=[UIColor redColor]; [self addSubview:_contentLb]; //取消按钮 _cancelBt=[[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_contentLb.frame), self.bounds.size.width/2, 50)]; _cancelBt.layer.borderColor=[UIColor grayColor].CGColor; _cancelBt.layer.borderWidth=0.5; [_cancelBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_cancelBt addTarget:self action:@selector(cancelBtClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_cancelBt]; //确定按钮 _sureBt=[[UIButton alloc]initWithFrame:CGRectMake(self.bounds.size.width/2, CGRectGetMaxY(_contentLb.frame), self.bounds.size.width/2, 50)]; _sureBt.layer.borderColor=[UIColor grayColor].CGColor; _sureBt.layer.borderWidth=0.5; [_sureBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_sureBt addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_sureBt]; } return self;}#pragma mark----实现类方法+(instancetype)alterViewWithTitle:(NSString *)title content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure cancelBtClcik:(cancelBlock)cancelBlock sureBtClcik:(sureBlock)sureBlock;{ SZKAlterView *alterView=[[SZKAlterView alloc]initWithFrame:CGRectMake(0, 0, 250, 150)]; alterView.backgroundColor=[UIColor whiteColor]; alterView.center=CGPointMake(WIDTH/2, HEIGHT/2); alterView.layer.cornerRadius=5; alterView.layer.masksToBounds=YES; alterView.title=title; alterView.content=content; alterView.cancel=cancel; alterView.sure=sure; alterView.cancel_block=cancelBlock; alterView.sure_block=sureBlock; return alterView;}#pragma mark--给属性重新赋值-(void)setTitle:(NSString *)title{ _titleLb.text=title;}-(void)setContent:(NSString *)content{ _contentLb.text=content;}-(void)setSure:(NSString *)sure{ [_sureBt setTitle:sure forState:UIControlStateNormal];}-(void)setCancel:(NSString *)cancel{ [_cancelBt setTitle:cancel forState:UIControlStateNormal];}#pragma mark----取消按钮点击事件-(void)cancelBtClick{ [self removeFromSuperview]; self.cancel_block();}#pragma mark----确定按钮点击事件-(void)sureBtClick{ [self removeFromSuperview]; self.sure_block();}
需要注意的是:
因为笔者将该view封装成了类方法,所以会调用这个类时会先执行+(instancetype)alterViewWithTitle:(NSString *)title...
方法,紧接着执行-(instancetype)initWithFrame:(CGRect)frame
方法,然后会继续执行alterView.title=title;
属性赋值的方法,但是这时界面展示不出来内容的,需要在set方法中重新给相关内容赋值才会展示出来,最后俩个函数就是利用block将点击事件传递到外部类中。
大致的实现方法写到这里,如果有不明白的可以联系笔者,笔者有问必答,如果想要下载完整demo请点击:
SZKAlterView链接:https://github.com/18811314750/SZKAlterView
如果觉得笔者写的不错,可以star下哟。
如果各位观众老爷还没有看够,还有一篇关于无限轮播图的封装
iOS开发-两句代码快速实现无限轮播图(基于ScrollView封装)
http://www.jianshu.com/p/d240bd977689
笔者的其他文章:
iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)
http://www.jianshu.com/p/4e0ac2898de0
iOS开发-一句代码调用实现网络的监测功能(基于AFNetworkReachabilityManager的封装)
http://www.jianshu.com/p/b901ad0c1d81
如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到后第一时间回复。
QQ/微信:790057066 。
简书号:iOS_凯:http://www.jianshu.com/users/86b0ddc92021/latest_articles
GitHub个人主页:https://github.com/18811314750
欢迎各位前来查看,star,感谢各位的阅读。
作者:iOS_凯
链接:http://www.jianshu.com/p/de2ecfd770c2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- iOS开发-轻松学会封装自定义视图view(自定义弹框封装详解)
- 自定义View封装
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- IOS xib封装自定义view 的封装步骤
- iOS自定义按钮button详解封装说明
- 自定义View(四)之提取和封装自定义View
- iOS 自定义TableViewCell封装
- iOS自定义封装tabBar
- 使用xib封装自定义view
- 自定义View(三)——封装自定义View,通过封装类实现文字滚动、画圆
- 自定义视图:视图的封装过程.
- 自定义组件之【柱状图】详解 已封装成View
- 自定义View的封装和xib文件的使用详解
- iOS-AFNetworking 封装(自定义版)
- iOS编程-------自定义视图CustomView 封装视图LTView 视图控制器UIViewController
- IOS封装自定义Cell方法
- 【重磅推荐】MySQL大表优化方案(最全面)
- ant和maven的区别
- avocado学习笔记
- TCP协议
- linux下用top命令查看cpu利用率超过100%
- iOS开发-轻松学会封装自定义视图view(自定义弹框封装详解)
- Android Studio(2.3.3)配置Kotlin笔记
- java基础复习--复习总结1
- 项目框架总结
- 【LeetCode】150. Evaluate Reverse Polish Notation
- iOS json格式转换
- LeetCode
- 矩阵快速幂 hdu1757
- 下载Nexus老版本war包