当前位置:编程学习 > wap >>

UIButton的点击事件写成Block,AlertView的delegate写成Block

[plain] 
  
[plain]  
  
每次使用UIButton的点击事件都要新写一个方法,感觉太麻烦,今天下来无事就用block封装了一下button的操作事件,这样以后能省下一些时间,而且代码量也减少了不少。
下面我写上源码:
 
我写的是UIButton的类目
 
h文件:
 
[plain]  
<p class="p1"><span class="s1">#import </span><UIKit/UIKit.h></p><p class="p2">  
</p><p class="p3"><span class="s2">typedef</span> <span class="s2">void</span> (^ActionBlock)();</p><p class="p3"><span class="s2">typedef</span> <span class="s2">void</span> (^Block_SureEvent)();</p><p class="p2">  
</p><p class="p3"><span class="s2">@inte易做图ce</span> UIButton (Block)<<span class="s3">UIAlertViewDelegate</span>></p><p class="p3">-(<span class="s2">void</span>)handleClickEvent:(<span class="s3">UIControlEvents</span>)aEvent withClickBlick:(<span class="s4">ActionBlock</span>)buttonClickEvent;</p><p class="p3">-(<span class="s2">void</span>)AlertViewTitle:(<span class="s3">NSString</span> *)aTitle message:(<span class="s3">NSString</span> *)aMsg cancleButtonTitle:(<span class="s3">NSString</span> *)aButtonTitle sureButtonTitle:(<span class="s3">NSString</span> *)aSureButton sureBlockEvent:(<span class="s4">Block_SureEvent</span>)aSureEvent;</p><p class="p4">@end</p>  
 下面是m文件
[objc]  
#import "UIButton+Block.h"  
#import <objc/runtime.h>  
static charchar *overViewKey;  
  
@implementation UIButton (Block)  
-(void)handleClickEvent:(UIControlEvents)aEvent withClickBlick:(ActionBlock)buttonClickEvent  
{  
    objc_setAssociatedObject(self, &overViewKey, buttonClickEvent, OBJC_ASSOCIATION_COPY_NONATOMIC);  
    [self addTarget:self action:@selector(buttonClick) forControlEvents:aEvent];  
}  
-(void)buttonClick  
{  
    ActionBlock blockClick = objc_getAssociatedObject(self, &overViewKey);  
    if (blockClick != nil)  
    {  
        blockClick();  
    }  
}  
@end  
其中要想使用关联对象的objc_setAssociatedObject和objc_getAssociatedObject必须引用头文件objc/runtime.h
objc_setAssociatedObject的四个参数分别为:源对象,key[必须是 static char类型],关联对象,关联方式【与修饰属性类似】
objc_getAssociatedObject的两个参数:源对象,key。通过这两个参数可以获取关联的对象。
 
--------------------------------AlertView----------------------Block-----------------------------------------
 
[objc]  
<p class="p1"><span class="s1">static</span> <span class="s1">char</span> * alertKey;</p>  
[objc]  
-(void)AlertViewTitle:(NSString *)aTitle message:(NSString *)aMsg cancleButtonTitle:(NSString *)aButtonTitle sureButtonTitle:(NSString *)aSureButton sureBlockEvent:(Block_SureEvent)aSureEvent  
{  
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:aTitle message:aMsg delegate:self cancelButtonTitle:aButtonTitle otherButtonTitles:aSureButton, nil nil];  
    objc_setAssociatedObject(self, &alertKey, aSureEvent, OBJC_ASSOCIATION_COPY_NONATOMIC);  
    [alert show];  
}  
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
{  
    if (buttonIndex == 1)  
    {  
          
        Block_SureEvent blockSure = objc_getAssociatedObject(self, &alertKey);  
        if (blockSure != nil)  
        {  
            blockSure();  
        }  
        else  
        {  
            NSLog(@"error");  
        }  
          
    }  
}  
 
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,