

如何扩展 Combine:以给 UIButton 设置 title 为例
source link: https://imtx.me/blog/how-to-extend-combine-set-title-for-uibutton/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何扩展 Combine:以给 UIButton 设置 title 为例
我的 PasteNow 用了 Apple 自带的 Combine 库来实现一些响应式的设计,因为之前有 RxSwift 的使用经验,因此上手 Combine 比较快,加上是系统自带的,用起来也非常顺手。于是我最近还在做另外一件事情:迁移其他旧项目的 RxSwift 代码至 Combine。因为 Combine 作为 iOS 13/macOS 10.15 就引入的系统库,现在已经到了可以普遍采用的程度了,是时候和 RxSwift 说再见了。
毕竟 RxSwift 发展了那么多年,生态还是比 Combine 要丰富一些。于是在从 RxSwift 迁移至 Combine 的过程中,我遇到了不少 RxSwift 可以非常方便做到的事情,但在 Combine 默认不太方便。好在我们可以去扩展 Combine,就让我用一个 UIButton 设置 title 的例子来说明这个吧。
比如 RxSwift 里可以非常方便地将一个值绑定到 UIButton 上去:
.bind(to: button.rx.title(for: .normal))
在 Combine 里面,就只能:
.sink(receiveValue: { title in
button.setTitle(title, for: .normal)
})
如果这个 button 还是 self 上的,那么还得麻烦来一个 weak self,不然会强引用。
.sink(receiveValue: { [weak self] title in
self?.button.setTitle(title, for: .normal)
})
真的是非常麻烦呢。但只需要写这样的一个扩展,就能轻松解决这个问题:
import UIKit import Combine extension Publisher where Self.Output == String, Self.Failure == Never { public func setTitle(on button: UIButton, state: UIControl.State) -> AnyCancellable { sink { title in button.setTitle(title, for: state) } } }
然后就可以快乐地这样调用了:
.setTitle(on: button, state: .normal)
希望这则小技巧可以帮助到你,欢迎交流讨论。
Recommend
-
102
website upgrading… 京ICP备110065...
-
34
README.md NGUIButtonInsetsExample Example app which shows how UIButton insets work
-
56
我们先从一个问题说起,小明同学接到产品的一个新需求:实现一个 UIButton ,要求在 normal、selected、highlighted 三种状态下展示不同文案。这简直太简单了,小明同学1分钟不到就实现了,关键代码如下:
-
9
iOS 14.0 brings us many improvements and new APIs using which the developers can write efficient code. One such API is the closure based action API instead of the addTarget method in UIControls. Yes, you heard me right—no more us...
-
9
云音乐大前端专栏UIButton 状态新解2020-10-14...
-
8
跳出手掌心——如何立即触发UIButton边界事件 21 May 2015 • 5 min. read • 2 Comments
-
38
A new way to style UIButton with UIButton.Configuration in iOS 15 17 Jun 2021 ⋅ 12 min read ⋅ iOS 15
-
9
Customizing UIButton in iOS 15Buttons are an essential element in iOS apps. If you are developing an app using UIKit, you will probably use UIButton class to create buttons. Creating a button is a straightforward process, but it becomes prob...
-
54
How to make a custom button style with UIButton.Configuration in iOS 15 13 Sep 2021 ⋅ 18 min read ⋅ iOS 15
-
6
iOS SDK: Difference between UIButton setTitleForState and UIButton titleLabel.text advertisements I have this issue with a custom UIView where...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK