Skip to content

Instantly share code, notes, and snippets.

@brunomunizaf
Last active November 20, 2020 04:46
Show Gist options
  • Save brunomunizaf/1a239b995534c0e4feb8c4f8441a22be to your computer and use it in GitHub Desktop.
Save brunomunizaf/1a239b995534c0e4feb8c4f8441a22be to your computer and use it in GitHub Desktop.
import UIKit
final class ViewController: UIViewController {
// MARK: UI
let label = UILabel()
let incrementButton = UIButton()
let decrementButton = UIButton()
// MARK: Properties
let viewModel = ViewModel()
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// MARK: Setup
view.addSubview(label)
view.addSubview(incrementButton)
view.addSubview(decrementButton)
label.translatesAutoresizingMaskIntoConstraints = false
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
incrementButton.backgroundColor = .red
incrementButton.setTitle("Increment", for: .normal)
incrementButton.translatesAutoresizingMaskIntoConstraints = false
incrementButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 50).isActive = true
incrementButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 100).isActive = true
incrementButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -100).isActive = true
incrementButton.addTarget(self, action: #selector(viewModel.increment), for: .touchUpInside)
decrementButton.backgroundColor = .red
decrementButton.setTitle("Decrement", for: .normal)
decrementButton.translatesAutoresizingMaskIntoConstraints = false
decrementButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 100).isActive = true
decrementButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 100).isActive = true
decrementButton.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -100).isActive = true
decrementButton.addTarget(self, action: #selector(viewModel.decrement), for: .touchUpInside)
// MARK: Binding
viewModel.countBox.bind { [weak self] in
self?.label.text = "\($0)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment