我有一个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)
}
Run Code Online (Sandbox Code Playgroud)
但是当我跑它并且击中开关时它崩溃并且没有打印任何东西.
编辑:这是我的ViewController和我的自定义类文件:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
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.
}
}
Run Code Online (Sandbox Code Playgroud)
Bha*_*asu 46
将UISwitch Reference添加到ViewController.swift文件中.
@IBOutlet var mySwitch: UISwitch
@IBOutlet var switchState: UILabel
Run Code Online (Sandbox Code Playgroud)
然后将目标事件添加到viewdidload方法中,如下所示
mySwitch.addTarget(self, action: #selector(ViewController.switchIsChanged(_:)), forControlEvents: UIControlEvents.ValueChanged)
Run Code Online (Sandbox Code Playgroud)
当翻转开关时,将触发UIControlEventValueChanged事件并调用stateChanged方法.
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.on {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0
func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
@objc func switchIsChanged(mySwitch: UISwitch) {
if mySwitch.isOn {
switchState.text = "UISwitch is ON"
} else {
switchState.text = "UISwitch is OFF"
}
}
Run Code Online (Sandbox Code Playgroud)
在http://sourcefreeze.com/uiswitch-tutorial-using-swift-in-ios8/中找到简要教程
GoZ*_*ner 19
简洁,甚至是简约,在编码风格中很重要.试试这个:
@IBAction func switchValueChanged (sender: UISwitch) {
advice.isInProduction = sender.on
print ("It's \(advice.isInProduction)!")
}
Run Code Online (Sandbox Code Playgroud)
在原始代码中,您可能崩溃,因为acsessabilitySwitch或未advice绑定(具有值nil).
小智 5
对于swift 3
@IBAction func switchValueChanged(_ sender: UISwitch) {
print(sender.isOn)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62022 次 |
| 最近记录: |