ios 如何将布尔值链接到 UISwitch 的开/关状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24714921/
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
How to link a boolean value to the on/off state of a UISwitch?
提问by user3832611
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:
我UISwitch
想在我编写的函数中控制布尔值。我查看了UISwitch
类型参考,它将开关的开/关状态的属性列为on
. 我试图在一个动作中使用它:
@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.
但是当我运行它并按下开关时,它崩溃了并且没有打印任何东西。
EDIT: Here is my ViewController and my custom class files:
编辑:这是我的 ViewController 和我的自定义类文件:
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:
视图控制器.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.
}
}
回答by Bharathi Devarasu
Add the UISwitch Reference into ViewController.swift file.
将 UISwitch 引用添加到 ViewController.swift 文件中。
@IBOutlet var mySwitch: UISwitch
@IBOutlet var switchState: UILabel
then add the target event into the viewdidload method like below
然后将目标事件添加到 viewdidload 方法中,如下所示
mySwitch.addTarget(self, action: #selector(ViewController.switchIsChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
When the switch is flipped the UIControlEventValueChanged event is triggered and the stateChanged method will be called.
当开关被翻转时,触发 UIControlEventValueChanged 事件并调用 stateChanged 方法。
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.on {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Swift 3.0
斯威夫特 3.0
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Swift 4.0
斯威夫特 4.0
@objc func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
find brief tutorial in http://sourcefreeze.com/uiswitch-tutorial-using-swift-in-ios8/
在http://sourcefreeze.com/uiswitch-tutorial-using-swift-in-ios8/ 中找到简短的教程
回答by GoZoner
Succinctness, even parsimony, is important in coding style. Try this:
简洁,甚至简洁,在编码风格中很重要。尝试这个:
@IBAction func switchValueChanged (sender: UISwitch) {
advice.isInProduction = sender.on
print ("It's \(advice.isInProduction)!")
}
In your original code, you likely crashed because acsessabilitySwitch
or advice
are unbound (have values of nil
).
在您的原始代码中,您可能因为acsessabilitySwitch
或未advice
绑定(值为nil
)而崩溃。
[Updated - replaced println
with print
]
[更新 - 替换println
为print
]
回答by Hamza üzümcü
回答by Swift Developer
Drag and drop UISwitch from Object Library-
从对象库中拖放 UISwitch-
@IBOutlet-
@IBOutlet-
@IBOutlet weak var myswitch: UISwitch!
@IBAction-
@IBAction-
@IBAction func onAllAccessory(sender: UISwitch) {
if myswitch.on == true{
onCall()
}
if myswitch.on == false{
offCall()
}
}
Your function-
你的功能-
func onCall(){
print("On is calling")
}
func offCall(){
print("Off is calling")
}
回答by An. Jorge
回答by Mehdico
No need for extra boolean.
use this :
不需要额外的布尔值。
用这个 :
if mySwitch.isOn {
// do something
} else {
// do something else
}
this is an always up to date boolean for switch state, so NO NEEDto create your own variable.
这是用于开关状态的始终最新的布尔值,因此无需创建自己的变量。
回答by Anton Lindroth
If you are trying to change the switch you can use
如果您正在尝试更改开关,您可以使用
mySwitch.on = true
to set it to "on" or
将其设置为“开”或
mySwitch.off == false
to set it to "off"
将其设置为“关闭”
Or if you want to get the value you can use
或者,如果您想获得可以使用的价值
let myBool = mySwitch.on
If the switch is powered on the value will be true, otherwise false.
如果交换机通电,该值将为真,否则为假。
You can also make an action called valueChanged (to get or update the value whenever the switch value is changed)
您还可以执行一个名为 valueChanged 的操作(每当开关值更改时获取或更新值)
@IBAction func valueChanged(sender: AnyObject) {
// some code
}
You can do this by select the dualView... view and ctrl drag from your switch to you view controller and choose action, and value changed if that's not already selected.
您可以通过选择 dualView... 视图并按住 ctrl 从您的开关拖动到您的视图控制器并选择操作来完成此操作,如果尚未选择,则更改值。