如何在swift中单击按钮时更改开关状态

时间:2014-09-03 03:16:52

标签: ios xcode swift switch-statement

我正在制作一个基本的应用程序,我可以打开和关闭开关,告诉我是否在一天的某个课程中有作业。我遇到的问题是重置按钮,将所有开关更改为关闭。如何使用按钮在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


}

1 个答案:

答案 0 :(得分:14)

应该只使用交换机的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);
}