2014-09-03 78 views
1

我正在製作一個基本應用程序,可以打開和關閉開關以告訴我是否在一天中某個班級中有作業。我遇到的問題是將所有開關都關閉的重置按鈕。如何使用按鈕快速更改開關狀態?這是我到目前爲止:如何在swift中單擊某個按鈕時更改開關狀態

// 
// 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 


} 

回答

9

應該僅僅是使用交換機的setOn()方法的問題:

@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); 
}