SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //: A UIKit based Playground for presenting user interface
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5.  
  6. class MyViewController : UIViewController {
  7.  
  8.     let label = UILabel()
  9.     let button = UIButton(type: .roundedRect)
  10.     let viewModel = MyViewModel()
  11.    
  12.     override func loadView() {
  13.         setupUI()
  14.     }
  15.    
  16.     func setupUI(){
  17.         let view = UIView()
  18.         view.backgroundColor = .white
  19.         label.frame = CGRect(x: 60, y: 200, width: 300, height: 40)
  20.         label.text = "Weather?"
  21.         label.textColor = .black
  22.        
  23.         button.frame = CGRect(x:60, y: 250, width: 200, height: 40)
  24.         button.setTitle("Tap me!", for: .normal)
  25.         button.addTarget(self, action: Selector(("buttonTap:")), for: .touchUpInside)
  26.         button.backgroundColor = .clear
  27.         button.tintColor = .black
  28.         button.layer.cornerRadius = 15
  29.         button.layer.borderWidth = 3
  30.         button.layer.borderColor = UIColor.black.cgColor
  31.        
  32.         view.addSubview(label)
  33.         view.addSubview(button)
  34.         self.view = view
  35.     }
  36.    
  37.     // button tap action
  38.     @objc func buttonTap(_ sender: AnyObject) {
  39.         viewModel.getWeather()
  40.         viewModel.update(label: label)
  41.     }
  42. }
  43.  
  44.  
  45. class MyViewModel {
  46.    
  47.     //updateUI
  48.     func update(label: UILabel){
  49.         label.text = "It is \(getWeather().description) today and \(getWeather().tempature) °C"
  50.     }
  51.    
  52.     //get weather data from somewhere
  53.     func getWeather() -> Weather{
  54.         return Weather()
  55.     }
  56. }
  57.  
  58.  
  59. struct Weather {
  60.     var tempature: Double = 25.0
  61.     var description: String = "Raining"
  62. }
  63.  
  64. // Present the view controller in the Live View window
  65. PlaygroundPage.current.liveView = MyViewController()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top