xcode 快速单击按钮时如何更改开关状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25635708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:28:41  来源:igfitidea点击:

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

iosxcodeswiftswitch-statement

提问by Nathan parker

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


}

回答by Mike S

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

这应该只是使用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);
}