Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a switch state when a button is clicked in swift

I am just making a basic app that I can turn on and off switches to tell me if I have homework in a certain class throughout the day. The problem I am having is the reset button that changes all of the switches to off. How can I change a switch state in swift with a button? This is what I had so far:

//
//  ViewController.swift
//  HomeworkManager
//
//  Created by Nate Parker on 9/2/14.
//  Copyright (c) 2014 Nathan Parker. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func resetClicked(sender: AnyObject) {
}

@IBOutlet var spanish: UISwitch
@IBOutlet var algebra: UISwitch
@IBOutlet var amerCult: UISwitch
@IBOutlet var bio: UISwitch
@IBOutlet var col: UISwitch


}
like image 941
Nathan parker Avatar asked Dec 05 '22 05:12

Nathan parker


1 Answers

It should just be a matter of using the setOn() method of the switches:

@IBAction func resetClicked(sender: AnyObject) {
    spanish.setOn(false, animated: true);
    algebra.setOn(false, animated: true);
    amerCult.setOn(false, animated: true);
    bio.setOn(false, animated: true);
    col.setOn(false, animated: true);
}
like image 200
Mike S Avatar answered Dec 30 '22 12:12

Mike S