前言:眾所周知哩牍,現(xiàn)在的UIAlertView和UISheetView合并組成了UIAlertController。前幾天用到了UIAlertController的UIAlertControllerStyleActionSheet類(lèi)型利术,才發(fā)現(xiàn)如果要展示多條數(shù)據(jù)的話,要寫(xiě)好長(zhǎng)代碼哪审,并且每個(gè)Action的block內(nèi)部都要做響應(yīng)的操作的話就會(huì)讓代碼看起來(lái)非常的繁瑣且凌亂。在此前提下奔浅,小編想到了封裝,如果我自己封裝一個(gè)SheetView裙戏,能夠用起來(lái)不這么繁瑣就好了。
原理:只需要?jiǎng)?chuàng)建的時(shí)候傳入title和message厕诡,然后設(shè)置一下Action的title累榜,采用代理的方式傳入Action的類(lèi)型,并將Action的事件使用代理傳遞灵嫌。
下面給大家詳細(xì)介紹:
首先壹罚,我是創(chuàng)建了一個(gè)類(lèi)繼承于NSObject,由于方法參數(shù)使用到了UI控件寿羞,所以要導(dǎo)入U(xiǎn)IKit框架猖凛。在.h文件中設(shè)置代理協(xié)議及方法,并聲明相關(guān)參數(shù)和接口方法绪穆。代碼如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol RHSheetViewDelegate;
@interface RHSheetView : NSObject
//聲明代理
@property (nonatomic, weak) id<RHSheetViewDelegate> delegate;
//Action的title數(shù)組
@property (nonatomic, strong) NSArray * actionTitles;
//自定義構(gòu)造方法
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message;
//跳轉(zhuǎn)展示方法
- (void)showWithController:(UIViewController *)controller;
@end
//設(shè)置代理協(xié)議及方法
@protocol RHSheetViewDelegate <NSObject>
@optional //非必須實(shí)現(xiàn)代理方法
//返回Action的樣式
- (UIAlertActionStyle)sheetView:(RHSheetView *)sheetView actionStyleAtIndex:(NSInteger)index;
//點(diǎn)擊相應(yīng)的Action之后觸發(fā)的方法
- (void)sheetView:(RHSheetView *)sheetView didSelectedAtIndex:(NSInteger)index;
@end
然后辨泳,在.m文件中聲明一個(gè)私有的UIAlertController全局對(duì)象,實(shí)現(xiàn).h中聲明相應(yīng)的方法及屬性的setter方法玖院。代碼如下:
#import "RHSheetView.h"
@interface RHSheetView ()
//聲明UIAlertController對(duì)象
@property (nonatomic, strong) UIAlertController * alert;
@end
@implementation RHSheetView
#pragma mark - public
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message {
self = [super init];
if (self) {
//構(gòu)造方法中創(chuàng)建聲明的對(duì)象
_alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
}
return self;
}
- (void)showWithController:(UIViewController *)controller {
//在相應(yīng)的Controller中彈出彈窗
[controller presentViewController:_alert animated:YES completion:nil];
}
#pragma mark - setter
//重寫(xiě)聲明的數(shù)組屬性的setter方法
- (void)setActionTitles:(NSArray *)actionTitles {
//對(duì)屬性賦值
_actionTitles = actionTitles;
//for循環(huán)創(chuàng)建Action
for (int i = 0; i < actionTitles.count; i++) {
//如果通過(guò)代理返回了Action的樣式菠红,則根據(jù)返回的樣式設(shè)置相應(yīng)Action的樣式
if ([self.delegate respondsToSelector:@selector(sheetView:actionStyleAtIndex:)]) {
UIAlertActionStyle style = [self.delegate sheetView:self actionStyleAtIndex:i];
UIAlertAction * action = [UIAlertAction actionWithTitle:actionTitles[i] style:style handler:^(UIAlertAction * _Nonnull action) {
//在此設(shè)置Action的點(diǎn)擊代理方法
if ([self.delegate respondsToSelector:@selector(sheetView:didSelectedAtIndex:)]) {
[self.delegate sheetView:self didSelectedAtIndex:i];
}
//點(diǎn)擊之后返回到當(dāng)前界面
[_alert dismissViewControllerAnimated:YES completion:nil];
}];
[_alert addAction:action];
}else {
//如果沒(méi)有返回Action樣式,則默認(rèn)設(shè)置為UIAlertActionStyleDefault
UIAlertAction * action = [UIAlertAction actionWithTitle:actionTitles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//在此設(shè)置Action的點(diǎn)擊代理方法
if ([self.delegate respondsToSelector:@selector(sheetView:didSelectedAtIndex:)]) {
[self.delegate sheetView:self didSelectedAtIndex:i];
}
//點(diǎn)擊之后返回到當(dāng)前界面
[_alert dismissViewControllerAnimated:YES completion:nil];
}];
[_alert addAction:action];
}
}
}
OK难菌!到此封裝就結(jié)束了试溯,接下來(lái)就是實(shí)際應(yīng)用了。在應(yīng)用中無(wú)論需要添加多少個(gè)Action郊酒,我們要寫(xiě)的代碼長(zhǎng)度都差不多遇绞,小編舉了個(gè)例子具體如下:
#import "ViewController.h"
#import "RHSheetView.h"
@interface ViewController () <RHSheetViewDelegate>
@property (nonatomic, strong) UIButton * btn;
@property (nonatomic, strong) RHSheetView * sheet;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self test];
}
//測(cè)試創(chuàng)建了一個(gè)button
- (void)test {
_btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 50)];
_btn.center = self.view.center;
_btn.backgroundColor = [UIColor lightGrayColor];
[_btn setTitle:@"選擇更改" forState:UIControlStateNormal];
[_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btn];
}
- (void)btnClick:(UIButton *)btn {
//button點(diǎn)擊彈出SheetView
[self.sheet showWithController:self];
}
//懶加載創(chuàng)建(這個(gè)是小編習(xí)慣的創(chuàng)建方式)
- (RHSheetView *)sheet {
if (!_sheet) {
_sheet = [[RHSheetView alloc] initWithTitle:@"請(qǐng)選擇支付方式" message:@""];
_sheet.delegate = self;
_sheet.actionTitles = @[@"支付寶支付", @"微信支付", @"取消"];
}
return _sheet;
}
#pragma mark - 代理方法實(shí)現(xiàn)
//返回Action的樣式
- (UIAlertActionStyle)sheetView:(RHSheetView *)sheetView actionStyleAtIndex:(NSInteger)index {
if (index == 0) {
return UIAlertActionStyleDestructive;
}else if (index == sheetView.actionTitles.count - 1) {
return UIAlertActionStyleCancel;
}
return UIAlertActionStyleDefault;
}
//Action點(diǎn)擊觸發(fā)的事件
- (void)sheetView:(RHSheetView *)sheetView didSelectedAtIndex:(NSInteger)index {
if (index < sheetView.actionTitles.count - 1) {
[_btn setTitle:sheetView.actionTitles[index] forState:UIControlStateNormal];
}
}
@end
在此小編截圖了幾張給大家看看效果
如果還有什么不清晰的地方,小編做了Demo已上傳到GitHub上邊燎窘,大家可以去下載看看摹闽,git下載地址:https://github.com/guorenhao/SheetView.git
最后,還是希望能夠幫到有需要的朋友們褐健,愿同是程序猿的我們能夠共同學(xué)習(xí)進(jìn)步钩骇,在開(kāi)發(fā)的道路上越走越遠(yuǎn)!謝謝铝量!