LXMBlockKit 0.0.8

LXMBlockKit 0.0.8

测试测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2020年3月

Phelthas 维护。



  • 作者
  • Phelthas

LXMBlockKit

为 UIKit 添加了一些类别和 block API

UIButton 的 target-action 方法不能把回调方法跟配置方法写在一起,不利于代码阅读;而且 target 也基本上都是所属的 viewController,每次都 addTarget:self 也很麻烦;平常使用来说,button 用的最多的也就是响应一下 touchUpInside 事件,所以封装一个用 block 回调点击事件的方法就能很好的应对日常的使用~
类似的情况还有 UIBarButtonItem,UIGestureRecognizer,NSNotification 等
所以就有了这个库

原来 button 事件的典型写法:

[testButton addTarget:self action:@selector(handleButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

- (void)handleButtonTapped:(UIButton *)sender {    
    NSLog(@"handleButtonTapped");        
}

使用 Block 的写法:

   [testButton addButtonCallback:^(UIButton *sender) {
        NSLog(@"handleButtonTapped");
   }];

可以看出还是省了很多代码的,而且事件的回调就写在 button 的配置处,不用来回找~~

需要注意的是: __weak typeof(self) weakSelf = self;
block 回调中,务必使用 weakSelf!直接使用 self 必定会导致循环引用!!!
block 回调中,务必使用 weakSelf!直接使用 self 必定会导致循环引用!!!
block 回调中,务必使用 weakSelf!直接使用 self 必定会导致循环引用!!!
重要的事情说三遍!!!
这是所有 block 都需要注意的问题,没办法,只能自己在脑中时刻提醒自己注意,要不就跑 Instruments 测试吧~

其中NSNotificationCenter的部分,基本是照搬大神nicklockwood的FXNotifications;
研究了几个类似的可以 autoRemove 的 notification 的库,感觉还是 FXNotifications 最符合平时的场景,所以就按自己的代码习惯重新敲了一遍

有什么问题,欢迎讨论~

如何使用

1, cocoaPods
在你的 podfile 中添加
pod 'LXMBlockKit', '~> 0.0.6'
运行 pod installpod update
然后在需要的地方 #import "LXMBlockKit.h" 就可以了

更新

0.0.6   加入 NSTimer 的 block 方法
0.0.5   加入 nullable 和 nonnull 等字段,方便 swift 调用
0.0.4   更新 UIBarButtonItem 方法
0.0.3 修复 target 为 nil 的 bug

许可

LXMBlockKit 在 MIT 许可下提供。有关详细信息,请参阅 LICENSE 文件。