《iOS开发笔记—自定义UIAlertController》
2017-05-24 16:42
253 查看
一、自定义UIAlertController
UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet。
下面我们就自己来定义一个UIAlertController类。
该分类实现了遮盖住UIAlertController 的 视图。
1、UIAlertController分类的抽取
.h文件
#import <UIKit/UIKit.h>
@interface UIAlertController (category)
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers;
@end
.m文件
#import "UIAlertController+category.h"
#import <objc/runtime.h>
static void * containerViewPorpertyKey = (void *)@"containerViewPorpertyKey";
CGFloat padding = 10;
CGFloat itemHeight = 57;
CGFloat lineHeight = 0.5;
CGFloat itemCount = 2;
@interface UIAlertController ()
@property (nonatomic, retain) UIView *containerView;
@end
@implementation UIAlertController (category)
- (id)containerView // containerView
{
return objc_getAssociatedObject(self, containerViewPorpertyKey);
}
- (void)setContainerView:(id)containerView
{
objc_setAssociatedObject(self, containerViewPorpertyKey, containerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers{
// 视图
CGFloat alertVCWidth = self.view.frame.size.width - 2 * padding;
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertVCWidth, itemHeight*itemCount + (itemCount-1)*lineHeight)];
[self.view addSubview: self.containerView];
if (titles.count-1 > 0) {
for (int i = 0; i< titles.count-1; i++) {
UILabel *l = [[UILabel alloc] init];
l.frame = CGRectMake(padding, (itemHeight+lineHeight)*i, alertVCWidth - 2 * padding, itemHeight);
l.backgroundColor = [UIColor clearColor];
l.text = titles[i];
l.font = [UIFont systemFontOfSize:30];
l.textAlignment = NSTextAlignmentCenter;
l.textColor = [UIColor blackColor];
l.userInteractionEnabled = false;
[self.containerView addSubview: l];
}
}
// actions
if (actionHandlers.count-1 > 0) {
for (int i = 0; i< actionHandlers.count-1; i++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler: actionHandlers[i]];
[self addAction: action];
}
}
// 取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:titles[titles.count-1] style:UIAlertActionStyleCancel handler: actionHandlers[actionHandlers.count-1]];
[self addAction:cancelAction];
}
@end
2、分类使用
#import "ViewController.h"
#import "UIAlertController+category.h"
@interface ViewController ()
@property(nonatomic, retain) UIAlertController *alertVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSArray *titles = @[@"保存", @"保存222", @"123", @"取消"];
NSArray *actionHandles = @[
^(UIAlertAction *action){
NSLog(@"1");
},
^(UIAlertAction *action){
NSLog(@"2");
},
^(UIAlertAction *action){
NSLog(@"3");
},
^(UIAlertAction *action){
NSLog(@"取消");
}
];
[self.alertVC configTitles:titles withActionHandlers:actionHandles];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self presentViewController:self.alertVC animated:YES completion:nil];
}
@end
UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet。
下面我们就自己来定义一个UIAlertController类。
该分类实现了遮盖住UIAlertController 的 视图。
1、UIAlertController分类的抽取
.h文件
#import <UIKit/UIKit.h>
@interface UIAlertController (category)
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers;
@end
.m文件
#import "UIAlertController+category.h"
#import <objc/runtime.h>
static void * containerViewPorpertyKey = (void *)@"containerViewPorpertyKey";
CGFloat padding = 10;
CGFloat itemHeight = 57;
CGFloat lineHeight = 0.5;
CGFloat itemCount = 2;
@interface UIAlertController ()
@property (nonatomic, retain) UIView *containerView;
@end
@implementation UIAlertController (category)
- (id)containerView // containerView
{
return objc_getAssociatedObject(self, containerViewPorpertyKey);
}
- (void)setContainerView:(id)containerView
{
objc_setAssociatedObject(self, containerViewPorpertyKey, containerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers{
// 视图
CGFloat alertVCWidth = self.view.frame.size.width - 2 * padding;
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertVCWidth, itemHeight*itemCount + (itemCount-1)*lineHeight)];
[self.view addSubview: self.containerView];
if (titles.count-1 > 0) {
for (int i = 0; i< titles.count-1; i++) {
UILabel *l = [[UILabel alloc] init];
l.frame = CGRectMake(padding, (itemHeight+lineHeight)*i, alertVCWidth - 2 * padding, itemHeight);
l.backgroundColor = [UIColor clearColor];
l.text = titles[i];
l.font = [UIFont systemFontOfSize:30];
l.textAlignment = NSTextAlignmentCenter;
l.textColor = [UIColor blackColor];
l.userInteractionEnabled = false;
[self.containerView addSubview: l];
}
}
// actions
if (actionHandlers.count-1 > 0) {
for (int i = 0; i< actionHandlers.count-1; i++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler: actionHandlers[i]];
[self addAction: action];
}
}
// 取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:titles[titles.count-1] style:UIAlertActionStyleCancel handler: actionHandlers[actionHandlers.count-1]];
[self addAction:cancelAction];
}
@end
2、分类使用
#import "ViewController.h"
#import "UIAlertController+category.h"
@interface ViewController ()
@property(nonatomic, retain) UIAlertController *alertVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSArray *titles = @[@"保存", @"保存222", @"123", @"取消"];
NSArray *actionHandles = @[
^(UIAlertAction *action){
NSLog(@"1");
},
^(UIAlertAction *action){
NSLog(@"2");
},
^(UIAlertAction *action){
NSLog(@"3");
},
^(UIAlertAction *action){
NSLog(@"取消");
}
];
[self.alertVC configTitles:titles withActionHandlers:actionHandles];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self presentViewController:self.alertVC animated:YES completion:nil];
}
@end
相关文章推荐
- 真Unity3d_NavMeshNavigation和自定义Shader
- Spring 中使用@Aspect 控制自定义注解
- [发布]LinScroll (jquery插件) (用图片自定义滚动条)
- Xcode如何自定义字段
- 自定义comparator
- 使用自定义的AlertDialog。
- jquery validate(4) : 写自定义验证
- 自定义一个jsp标签
- jquery1.9.1自定义插件简述
- 第三章 VIVADO 自定义IP 流水灯实验
- Android 自定义进度条
- 自定义view圆形之生成随机数改变
- string boot 与 自定义interceptor的实例讲解
- wpf日历控件制作过程分析(2)---自定义样式属性
- 自定义 URL Scheme 完全指南
- UIPageControl实现自定义按钮
- JSP自定义标签扩展----支持EL,访问Spring容器
- 自定义搜索帮助,类似C#的级联查询
- Bootstrap Popover(弹出框)弹出自定义格式代码
- 实现自定义android柱状图控件!可显示气温!可相应点击事件的回调!