hotwire-native-bridge-button #10
Replies: 1 comment 1 reply
-
Great article 🙌 If you want to make the icon optional, to be able to use either a text or an icon, you can do that like so: import HotwireNative
import UIKit
final class ButtonComponent: BridgeComponent {
override class var name: String { "button" }
override func onReceive(message: Message) {
guard let viewController else { return }
addButton(via: message, to: viewController)
}
private var viewController: UIViewController? {
delegate.destination as? UIViewController
}
private func addButton(via message: Message, to viewController: UIViewController) {
guard let data: MessageData = message.data() else { return }
- let image = UIImage(systemName: data.image)
+ let image: UIImage?
+
+ if let imageName = data.image {
+ image = UIImage(systemName: imageName)
+ } else {
+ image = nil
+ }
+
let action = UIAction { [unowned self] _ in
self.reply(to: "connect")
}
let item = UIBarButtonItem(title: data.title, image: image, primaryAction: action)
viewController.navigationItem.rightBarButtonItem = item
}
}
private extension ButtonComponent {
struct MessageData: Decodable {
let title: String
- let image: String
+ let image: String?
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
hotwire-native-bridge-button
The Hotwire Native Bridge Components docs demonstrate using a Button Component.
https://blog.corsego.com/hotwire-native-bridge-button
Beta Was this translation helpful? Give feedback.
All reactions