首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将布尔值链接到UISwitch的开/关状态?

如何将布尔值链接到UISwitch的开/关状态?
EN

Stack Overflow用户
提问于 2014-07-13 00:22:50
回答 5查看 70.6K关注 0票数 30

我有一个UISwitch,我想在我编写的函数中控制一个布尔值。我查看了UISwitch类型参考,它列出了开关的开/关状态的属性为on。我试着在一个动作中使用这个:

代码语言:javascript
复制
@IBAction func switchValueChanged(sender: UISwitch) {
        if acsessabilitySwitch.on {
//accessibilitySwitch is the UISwitch in question 
            println("It's True!")
            advice.isInProduction = Bool (true) 
// isInProduction is a attribute of a class
        } else {
            println("It's False!")
            advice.isInProduction = Bool (false)
        }

但当我运行它并按下开关时,它崩溃了,没有打印任何东西。

编辑:这是我的ViewController和我的自定义类文件:

BuyingAdviceModel.swift:

代码语言:javascript
复制
import Foundation
class videoGameModel{
    var price : Double
    var isInProduction : Bool
    var adviceGiven: String?
    init (isInProduction : Bool, price: Double){
        self.price = price
        self.isInProduction = isInProduction
    }
    func giveAdvice (price:Double, isInProduction:Bool)->(adviceGiven:String){
            if price >= 199.99 {
                var adviceGiven = "Nope, that's too expensive!"
                return adviceGiven
            } else if price <= 99.99{
                if isInProduction == true {
                    var adviceGiven = ("Buy it at GameStop!")
                    return adviceGiven
                } else {
                    var adviceGiven = ("Go look online!")
                    return adviceGiven
                }
            } else {
                var adviceGiven = ("Are you sure you put the info in correctly?")
                return adviceGiven
            }
    }
}

ViewController.swift:

代码语言:javascript
复制
import UIKit
import Foundation
class ViewController: UIViewController {
    @IBOutlet var priceTextField: UITextField
    @IBAction func adviceButtonTapped(sender: AnyObject) {
        let adviceOutputed = advice.adviceGiven!
        adviceLabel.text=adviceOutputed
    }
    @IBAction func viewTapped(sender: AnyObject) {
        priceTextField.resignFirstResponder()
    }

     @IBOutlet var acsessabilitySwitch: UISwitch
     @IBOutlet var adviceLabel: UILabel
     @IBAction func switchValueChanged (sender: UISwitch) {
        advice.isInProduction = sender.on
        println ("It's " + advice.isInProduction.description + "!")
    }
    var advice = videoGameModel (isInProduction: true,price: 0.00)
    func refreshUI(){
        priceTextField.text = String(advice.price)
        adviceLabel.text = ""
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        refreshUI()
    }

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


}
EN

回答 5

Stack Overflow用户

发布于 2014-07-13 00:46:46

简洁性,甚至简洁性,在编码风格中很重要。试试这个:

代码语言:javascript
复制
@IBAction func switchValueChanged (sender: UISwitch) {
  advice.isInProduction = sender.on
  print ("It's \(advice.isInProduction)!")
}

在您的原始代码中,您很可能因为acsessabilitySwitchadvice未绑定(值为nil)而崩溃。

更新-用print替换了println

票数 22
EN

Stack Overflow用户

发布于 2017-05-04 20:04:18

对于swift 3

代码语言:javascript
复制
@IBAction func switchValueChanged(_ sender: UISwitch) {
        print(sender.isOn)
}
票数 7
EN

Stack Overflow用户

发布于 2016-05-11 21:27:20

从对象库拖放UISwitch -

@IBOutlet-

代码语言:javascript
复制
@IBOutlet weak var myswitch: UISwitch!

@IBAction-

代码语言:javascript
复制
@IBAction func onAllAccessory(sender: UISwitch) {

        if myswitch.on == true{
            onCall()
        }
        if myswitch.on == false{
            offCall()
        }
    }

你的功能-

代码语言:javascript
复制
func onCall(){
        print("On is calling")
    }
    func offCall(){
        print("Off is calling")
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24714921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档