- Open
MainViewController.swift
and add autoIncrementButton
property
private var autoIncrementButton: UIButton!
- Create button
override func viewDidLoad() {
// ...
decrementButton.titleLabel?.font = .systemFont(ofSize: 30)
let autoIncrementButton = UIButton(primaryAction: UIAction(handler: { [weak self] _ in
guard let self else { return }
if self.store.state.isAutoIncrementEnabled {
self.store.dispatch(.stopAutoIncrement)
} else {
self.store.dispatch(.startAutoIncrement)
}
}))
autoIncrementButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(autoIncrementButton)
autoIncrementButton.topAnchor.constraint(equalTo: decrementButton.bottomAnchor, constant: 20).isActive = true
autoIncrementButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
autoIncrementButton.setTitle("Start", for: .normal)
autoIncrementButton.titleLabel?.font = .systemFont(ofSize: 30)
self.autoIncrementButton = autoIncrementButton
// ...
}
- Change subscribe closure to update button text
store.subscribe { [weak self] state in
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.label.text = "\(state.count)"
if state.isAutoIncrementEnabled {
self.autoIncrementButton.setTitle("Stop", for: .normal)
} else {
self.autoIncrementButton.setTitle("Start", for: .normal)
}
}
}
- Run app and tap "Start" button. Then the label will increase value every second.
- That's it!
Go to home page