如何将布尔值链接到UISwitch的开/关状态? [英] How to link a boolean value to the on/off state of a UISwitch?
问题描述
我有一个UISwitch
,我想在编写的函数中控制布尔值.我查看了UISwitch
类型参考,它以on
的形式列出了开关的开/关状态的属性.我试图在动作中使用它:
I have a UISwitch
that I want to control a boolean value in a function I wrote. I looked in the UISwitch
Type Reference and it listed the property for the on/off state of the switch as on
. I tried to use this in a action:
@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)
}
但是当我运行它并按下开关时,它崩溃了,什么也没打印.
but when I ran it and hit the switch it crashed and didn't print anything.
这是我的ViewController和自定义类文件:
Here is my ViewController and my custom class files:
BuyingAdviceModel.swift:
BuyingAdviceModel.swift:
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:
ViewController.swift:
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.
}
}
推荐答案
将UISwitch参考添加到ViewController.swift文件中.
Add the UISwitch Reference into ViewController.swift file.
@IBOutlet var mySwitch: UISwitch
@IBOutlet var switchState: UILabel
然后将目标事件添加到如下所示的viewdidload方法中
then add the target event into the viewdidload method like below
mySwitch.addTarget(self, action: #selector(ViewController.switchIsChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
按下开关时,将触发UIControlEventValueChanged事件,并将调用stateChanged方法.
When the switch is flipped the UIControlEventValueChanged event is triggered and the stateChanged method will be called.
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.on {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Swift 3.0
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Swift 4.0
@objc func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
在 http://sourcefreeze.com/uiswitch-tutorial中找到 简短教程-using-swift-in-ios8/
这篇关于如何将布尔值链接到UISwitch的开/关状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!