Skip to content

Instantly share code, notes, and snippets.

@hashemi
Created November 30, 2020 17:21
Show Gist options
  • Save hashemi/223539cd8b4661340fb7017e99478230 to your computer and use it in GitHub Desktop.
Save hashemi/223539cd8b4661340fb7017e99478230 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// Counter
//
// Created by Ahmad Alhashemi on 11/24/20.
//
import UIKit
class ViewController: UIViewController {
var counter: Int = 0 {
didSet {
UserDefaults.standard.set(counter, forKey: "counter")
configureView()
}
}
var maxAllowed: Int = 10
@IBOutlet weak var counterLabel: UILabel!
@IBOutlet weak var minusButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
counter = UserDefaults.standard.integer(forKey: "counter")
}
func configureView() {
counterLabel.text = String(counter)
minusButton.isEnabled = counter != 0
counterLabel.textColor = counter > maxAllowed ? .systemRed : .black
}
@IBAction func plusPressed(_ sender: Any) {
counter += 1
}
@IBAction func minusPressed(_ sender: Any) {
counter -= 1
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SettingsSegue" {
let s = segue.destination as! SettingsViewController
s.maxAllowed = maxAllowed
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment