Last active
October 8, 2017 21:40
-
-
Save palumbo/80cca5188165d11d83b19044da0f7a84 to your computer and use it in GitHub Desktop.
Swift 4 solution for controlling 2 UISwitch elements and UILabels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Basic | |
// | |
// Created by Joseph Palumbo on 10/8/17. | |
// Copyright © 2017 Joseph Palumbo. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var sw1: UISwitch! | |
@IBOutlet weak var sw2: UISwitch! | |
@IBOutlet weak var label1: UILabel! | |
@IBOutlet weak var label2: UILabel! | |
// controls first switch | |
@IBAction func switchDidChange(_ sender: UISwitch) { | |
if sw1.isOn { | |
sw2.setOn(false, animated: true) | |
label1.text = "ON" | |
label2.text = "OFF" | |
} | |
else { | |
sw2.setOn(true, animated: true) | |
label1.text = "OFF" | |
label2.text = "ON" | |
} | |
} | |
// controls second switch | |
@IBAction func switchDidChangeAlso(_ sender: UISwitch) { | |
if sw2.isOn { | |
sw1.setOn(false, animated: true) | |
label2.text = "ON" | |
label1.text = "OFF" | |
} else { | |
sw1.setOn(true, animated: true) | |
label2.text = "OFF" | |
label1.text = "ON" | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
if (sw1.isOn) { | |
label1.text = "ON" | |
} else { | |
label1.text = "OFF" | |
} | |
if (sw2.isOn) { | |
label2.text = "ON" | |
} else { | |
label2.text = "OFF" | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment