Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //: A UIKit based Playground for presenting user interface
- import UIKit
- import PlaygroundSupport
- class MyViewController : UIViewController {
- let label = UILabel()
- let button = UIButton(type: .roundedRect)
- let viewModel = MyViewModel()
- override func loadView() {
- setupUI()
- }
- func setupUI(){
- let view = UIView()
- view.backgroundColor = .white
- label.frame = CGRect(x: 60, y: 200, width: 300, height: 40)
- label.text = "Weather?"
- label.textColor = .black
- button.frame = CGRect(x:60, y: 250, width: 200, height: 40)
- button.setTitle("Tap me!", for: .normal)
- button.addTarget(self, action: Selector(("buttonTap:")), for: .touchUpInside)
- button.backgroundColor = .clear
- button.tintColor = .black
- button.layer.cornerRadius = 15
- button.layer.borderWidth = 3
- button.layer.borderColor = UIColor.black.cgColor
- view.addSubview(label)
- view.addSubview(button)
- self.view = view
- }
- // button tap action
- @objc func buttonTap(_ sender: AnyObject) {
- viewModel.getWeather()
- viewModel.update(label: label)
- }
- }
- class MyViewModel {
- //updateUI
- func update(label: UILabel){
- label.text = "It is \(getWeather().description) today and \(getWeather().tempature) °C"
- }
- //get weather data from somewhere
- func getWeather() -> Weather{
- return Weather()
- }
- }
- struct Weather {
- var tempature: Double = 25.0
- var description: String = "Raining"
- }
- // Present the view controller in the Live View window
- PlaygroundPage.current.liveView = MyViewController()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement