1、UIButton點(diǎn)擊事件用block代替
添加UIButton分類,在分類中添加如下代碼
在.h文件中添加
typedef void (^ClickButtonBlock)(UIButton *button);
/**
將Block點(diǎn)擊屬性與點(diǎn)擊事件進(jìn)行關(guān)聯(lián)
@param aEvent UIControlEvents
@param block 點(diǎn)擊事件
*/
- (void)handleClickEvent:(UIControlEvents) aEvent usingBlock:(ClickButtonBlock)block;
在.m文件中添加
#import <objc/runtime.h>
static char *overViewKey;
- (void)handleClickEvent:(UIControlEvents)aEvent usingBlock:(ClickButtonBlock)block {
objc_setAssociatedObject(self, &overViewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
// 給button增加點(diǎn)擊事件
[self addTarget:self action:@selector(buttonClicked:) forControlEvents:aEvent];
}
- (void)buttonClicked:(UIButton *)sender {
ClickButtonBlock clickedBlock = objc_getAssociatedObject(self, &overViewKey);
// 如果block存在則調(diào)用Blcok
if (clickedBlock != nil) {
clickedBlock(sender);
}
}
用到了runtime的objc_setAssociatedObject(關(guān)聯(lián))
函數(shù)
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
參數(shù)說明
id object :表示關(guān)聯(lián)者,是一個(gè)對象,變量名理所當(dāng)然也是object
const void *key :獲取被關(guān)聯(lián)者的索引key
id value :被關(guān)聯(lián)者,這里是一個(gè)block
objc_AssociationPolicy policy :關(guān)聯(lián)時(shí)采用的協(xié)議,有assign,retain,copy等協(xié)議
調(diào)用
[collectionBtn handleClickEvent:UIControlEventTouchUpInside usingBlock:^(UIButton *button) {
//點(diǎn)擊事件
}];
好處
1、在寫項(xiàng)目時(shí)候不用寫一堆全局變量來傳參,block可以捕獲局部變量到block內(nèi)部直接用
2、不用寫[self addTarget:self action:@selector(想函數(shù)名想半天:) forControlEvents:UIControlEventTouchUpInside ];這行代碼
有需要給view添加點(diǎn)擊事件的需求該方法也適用,需要簡單改改代碼
還可以給view添加個(gè)分類實(shí)現(xiàn)漸變色,相關(guān)文章:
(http://www.itdecent.cn/p/e7c9e94e165b)
(http://www.itdecent.cn/p/9f350164c766)
2.與webview交互
//再聲明webView時(shí)添加代理(WKScriptMessageHandler)
[self.webView.configuration.userContentController addScriptMessageHandler:self name:@"kScriptMessageHandlerWithname"];
//實(shí)現(xiàn)協(xié)議方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
if (message.body){
if ([message.name isEqualToString:@"kScriptMessageHandlerWithname"]){
//Code...
}
}
}
//移除代理
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"kScriptMessageHandlerWithname"];
}
3.在控制臺中打印中文
在iOS開發(fā)中,我們調(diào)試接口時(shí)最多的就是用NSLog 或者是打斷點(diǎn)來po 數(shù)據(jù),然而NSLog 輸出的數(shù)據(jù)中,中文是UFT-8格式顯示的,所以為了解決這個(gè)問題,今天給大家推薦一個(gè)非常好的方法,主要思路就是利用objc/runtime運(yùn)行時(shí)機(jī)制 來替換掉系統(tǒng)的控制臺輸出方法,然后將utf-8格式的字符轉(zhuǎn)換成中文格式。
一般情況下,我們在使用NSLog 和 %@ 輸出某個(gè)對象時(shí),就會調(diào)用這個(gè)對象的 description 方法,它的返回值就是 NSString 字符串類型,所以 description 默認(rèn)實(shí)現(xiàn)返回的格式是 <類名: 對象的內(nèi)存地址>,
在必要情況下,我們需要重寫description方法以達(dá)到改變輸出結(jié)果目的,覆蓋description方法的默認(rèn)實(shí)現(xiàn)。重寫完description方法后,再調(diào)用NSLog(@”%@”,p)時(shí)輸出結(jié)果不再是<類名: 內(nèi)存地址>,而是返回的字符串。
注意:千萬不要在 description 方法中同時(shí)使用 %@ 和 self,如果這樣使用了,那么最終會造成程序死循環(huán),原因是因?yàn)椋喝绻褂昧?@和self,代表要調(diào)用self的description方法,最終就是循環(huán)調(diào)用description方法。
方法:創(chuàng)建一個(gè)類
.h中什么都不用寫
.m中如下
#import <objc/runtime.h>
static inline void zxp_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@implementation NSString (ZXPUnicode)
- (NSString *)stringByReplaceUnicode {
NSMutableString *convertedString = [self mutableCopy];
[convertedString replaceOccurrencesOfString:@"\\U"
withString:@"\\u"
options:0
range:NSMakeRange(0, convertedString.length)];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
return convertedString;
}
@end
@implementation NSArray (ZXPUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
zxp_swizzleSelector(class, @selector(description), @selector(zxp_description));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(zxp_descriptionWithLocale:));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(zxp_descriptionWithLocale:indent:));
});
}
- (NSString *)zxp_description {
return [[self zxp_description] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale {
return [[self zxp_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self zxp_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
NSDictionary、NSSet同理