為App添加消息提示框
所有的App都會有消息提示笼裳,如簡單的提示框绰播、詢問框黍瞧。此文不是介紹如何自定義消息提示框,消息提示使用ios自帶的UIAlertView椎例、UIAlertController挨决。
也許有人立即想到,這太簡單了订歪,馬上寫給你看脖祈。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:actionTitles, nil];
[alert show];
//回調(diào)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//事件處理
}
相信大家一個App里面消息提示肯定會有好多,難道在每個使用的地方加上如下代碼刷晋?這樣不覺得重復(fù)的代碼比較多嗎盖高,我們常說代碼復(fù)用慎陵,是不是可以重新設(shè)計下,提升一下代碼復(fù)用性呢喻奥?當(dāng)然是可以的席纽。
在我看來,大多數(shù)的數(shù)據(jù)流轉(zhuǎn)撞蚕,業(yè)務(wù)邏輯都會扯到ViewController润梯,這樣就會使用Controller變胖,大神們都會了解MVC甥厦、MVP纺铭、MVVM設(shè)計模式,我理解不是很透刀疙,我也不多說什么了舶赔。我認(rèn)為一個好的App框架的設(shè)計是要滿足功能模塊化,低偶合谦秧,代碼復(fù)用性強顿痪,調(diào)用方便簡潔,可擴展性就很不錯了油够。
哈哈蚁袭,不扯遠了,入正題石咬。只需要在ViewController擴展幾個方法揩悄。
/*
消息提示,錯誤提示
*/
@interface PGBaseController (errorMsgView)
#pragma mark message
- (void)showMsg:(NSString *)szMsg;
- (void)showTitle:(NSString *)szTitle msg:(NSString *)szMsg;
- (void)showAskAlertTitle:(NSString *)title
message:(NSString *)message
tag:(NSInteger)tag
action:(void(^)(NSInteger alertTag, NSInteger actionIndex))block
cancelActionTitle:(NSString *)cancelTitle
otherActionsTitles:(NSString *)actionTitles,...;
@end
方法實現(xiàn)
#pragma mark message
- (void)showMsg:(NSString *)szMsg {
[self showTitle:nil msg:szMsg];
}
- (void)showTitle:(NSString *)szTitle msg:(NSString *)szMsg {
[self showAskAlertTitle:szTitle message:szMsg tag:0 action:nil cancelActionTitle:@"確定" otherActionsTitles:nil];
}
- (void)showAskAlertTitle:(NSString *)title
message:(NSString *)message
tag:(NSInteger)tag
action:(void(^)(NSInteger alertTag, NSInteger actionIndex))block
cancelActionTitle:(NSString *)cancelTitle
otherActionsTitles:(NSString *)actionTitles,... {
NSMutableArray *arrayTitles = [[NSMutableArray alloc] init];
[arrayTitles addObject:cancelTitle];
NSString *szActionTitle = nil;
va_list argumentList;
if(actionTitles) {
[arrayTitles addObject:actionTitles];
va_start(argumentList, actionTitles);
szActionTitle = va_arg(argumentList, NSString *);
while(szActionTitle) {
[arrayTitles addObject:szActionTitle];
szActionTitle = va_arg(argumentList, NSString *);
}
va_end(argumentList);
}
if(IOS8_LATER) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
for(NSInteger i = 0; i < arrayTitles.count; i++)
{
NSString *string = [arrayTitles objectAtIndex:i];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if(block)
{
block(tag, i);
}
}];
[alertController addAction:okAction];
}
[self presentViewController:alertController animated:YES completion:nil];
} else {
#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:actionTitles, nil];
alert.alertActionBlock = block;
[alert show];
#endif
}
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.alertActionBlock)
{
alertView.alertActionBlock(alertView.tag, buttonIndex);
}
}
#endif
仔細看代碼的同學(xué)會發(fā)現(xiàn)這句 alertView.alertActionBlock(alertView.tag, buttonIndex); 印象中UIAlertView沒有這么個東西9碛啤I拘浴!這是使用了…… 對焕窝,沒錯就是Catagory+runtime蹬挺。
UIAlertView+action.h
#import <UIKit/UIKit.h>
typedef void(^PGAlertActionBlock)(NSInteger alertTag, NSInteger actionIndex);
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
@interface UIAlertView (action)
@property(nonatomic, copy)PGAlertActionBlock alertActionBlock;
@end
#endif
UIAlertView+action.m
#import "UIAlertView+action.h"
#import <objc/runtime.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
//定義常量 必須是C語言字符串
static char *actionNameKey = "actionNameKey";
@implementation UIAlertView (action)
- (void)setAlertActionBlock:(PGAlertActionBlock)alertActionBlock
{
objc_setAssociatedObject(self, actionNameKey, alertActionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (PGAlertActionBlock)alertActionBlock
{
return objc_getAssociatedObject(self, actionNameKey);
}
@end
#endif
如何使用呢?只需在有需要顯示的地方調(diào)用下面的方法就可以實現(xiàn)相應(yīng)的功能它掂。
[self showMsg:@"消息內(nèi)容"];
[self showAskAlertTitle:@"標(biāo)題" message:@"提示的內(nèi)容" tag:0 action:^(NSInteger alertTag, NSInteger actionIndex) {
//事件響應(yīng)
if(actionIndex == 0) {
} else if(actionIndex == 1) {
}
} cancelActionTitle:@"取消" otherActionsTitles:@"確定",nil];
到現(xiàn)在已經(jīng)完成此模塊的設(shè)計巴帮,有沒有發(fā)現(xiàn),這樣使用挺方便的E扒铩榕茧!呵呵!客给!寫的不好勿噴用押。
共同學(xué)習(xí)進步!
上一節(jié):從零開始設(shè)計搭建ios App框架(二)
下一節(jié):錯誤提示頁面