當(dāng)一個(gè)控制器里面有很多UIAlertView 的時(shí)候祸泪,你要獲取當(dāng)前用戶操作的是那個(gè)UIAlertView 很是麻煩打颤,先你要設(shè)置他的tag 值生蚁,然后你在代理里面要判斷tag景东,要是有10個(gè),這時(shí)候你就凌亂了捆探,忘了自己設(shè)置了那個(gè)是哪個(gè)然爆。
那我們今天就把UIAlertView改成block回調(diào)方式站粟,大家可以參考參考黍图,順便把UIActionSheet,UIImagepickerViewcontroller 也寫成自己的blocks奴烙。
大家也跟我一起自定義個(gè)UIAlertView吧
還是先看下效果圖吧:
一.常規(guī)blocks封裝
1.新建一個(gè)繼承自NSObject的ICInfomationView
在ICInfomationView.h 文件中我們加入#import <UIKit/UIKit.h>
寫一個(gè)blocks 變量類型助被,我們下面使用
<pre>typedef void (^ClickAtIndexBlock)(NSInteger buttonIndex);</pre>
遵循 <UIAlertViewDelegate> 協(xié)議,因?yàn)槲覀冞€是用系統(tǒng)的
2.定義一個(gè)類方法切诀,我們這個(gè)方法接受我們在使用的時(shí)候傳入的參數(shù)
// 我們設(shè)置的參數(shù)名都是模仿系統(tǒng)的名字來的
<pre><code>+(UIAlertView )initWithTitle:(NSString)title message:(NSString *)messge cancleButtonTitle:(NSString )cancleButtonTitle OtherButtonsArray:(NSArray)otherButtons clickAtIndex:(ClickAtIndexBlock) clickAtIndex;
</code></pre>
在ICInfomationView.m文件中
在頭部我們定義一個(gè)static 的blocks 變量
static ClickAtIndexBlock _ClickAtIndexBlock;
-實(shí)現(xiàn)這個(gè)類方法
<pre><code>
+(UIAlertView )initWithTitle:(NSString)title message:(NSString *)messge cancleButtonTitle:(NSString )cancleButtonTitle OtherButtonsArray:(NSArray)otherButtons clickAtIndex:(ClickAtIndexBlock) clickAtIndex;
{
_ClickAtIndexBlock = [clickAtIndex copy];
UIAlertView *Al = [[UIAlertView alloc] initWithTitle:title message:messge delegate:self cancelButtonTitle:cancleButtonTitle otherButtonTitles: nil];
for (NSString *otherTitle in otherButtons) {
[Al addButtonWithTitle:otherTitle];
}
[Al show];
return Al;
}
</code></pre>
-實(shí)現(xiàn)UIAlertView 的兩個(gè)代理
<pre><code>
pragma mark UIAlertViewDelegate
+(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
_ClickAtIndexBlock(buttonIndex);
}
+(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
_ClickAtIndexBlock = nil;
}
</code></pre>
到此我們自己封裝的UIAlertView結(jié)束了揩环, 以后要是用的時(shí)候非常簡單,就一句代碼幅虑,再也不用tag值和遵守協(xié)議實(shí)現(xiàn)代理了
<pre></code>
[ICInfomationView initWithTitle:@"哈哈,我沒有使用代理丰滑,我是blocks" message:@"呵呵" cancleButtonTitle:@"好吧" OtherButtonsArray:@[@"嗯呢"] clickAtIndex:^(NSInteger buttonAtIndex) {
NSLog(@"click index ====%ld",(long)buttonAtIndex);
}];
</code></pre>
二.增加使用 runtime 封裝方法
runtime用的最多的就是關(guān)聯(lián)(Association)
1、建立關(guān)聯(lián)
objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
參數(shù)解釋
object:關(guān)聯(lián)對象(id 可以為任意對象)
key :常量鍵
value:值(id 可以是任意值)
objc_AssociationPolicy:對象關(guān)聯(lián)策略
解釋:任意對象可以通過一個(gè)常量鍵關(guān)聯(lián)任意值
2倒庵、獲得關(guān)聯(lián)
objc_getAssociatedObject(id object, const void *key)
object:關(guān)聯(lián)對象(id 可以為任意對象)
key :常量
解釋:通過一個(gè)鍵獲得一個(gè)對象關(guān)聯(lián)值
本程序中用到的兩個(gè)方法
<pre></code>
-(void)setClickBlock:(ClickAtIndexBlock)block{
objc_setAssociatedObject(self, IC_alertView_Block, block, OBJC_ASSOCIATION_COPY);
}
-(ClickAtIndexBlock)clickBlock{
return objc_getAssociatedObject(self, IC_alertView_Block);
}
</code></pre>
UIAlertView原生代理調(diào)用
<pre></code>
pragma mark UIAlertViewDelegate
+(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.clickBlock) {
alertView.clickBlock(buttonIndex);
}
}
</code></pre>
猛戳代碼github點(diǎn)擊代碼