2014-07-12 35 views
18

我有一个UISwitch,我想控制我写的函数中的布尔值。我查看了UISwitch类型参考,并将交换机的开/关状态属性列为on。我试图在动作中使用它:如何将布尔值链接到UISwitch的开/关状态?

@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:

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:

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. 
    } 


} 
+2

为什么你使用'acsessabilitySwitch'代替'sender'一个rgument?也许你忘了连接它的IBOutlet。另外,你为什么要做“布尔(真)”而不是“真”?最后,为什么不只是做'advice.isInProduction = sender.on'? – slazyk

回答

12

简洁,甚至吝啬,是非常重要的编码风格。试试这个:

@IBAction func switchValueChanged (sender: UISwitch) { 
    advice.isInProduction = sender.on 
    println ("It's " + advice.isInProduction.description + "!") 
} 

在你的原代码,你可能会崩溃,因为acsessabilitySwitchadvice是绑定(有nil值)。

+0

我实现了这个代码,当我点击开关时它仍然崩溃。我将在问题中发布完整的代码。 – user3832611

+2

Upvote for the sentence ***“简洁,甚至简约,在编码风格***和重要实施中都非常重要。 –

34

将UISwitch Reference添加到ViewController.swift文件中。

@IBOutlet var mySwitch: UISwitch 
@IBOutlet var switchState: UILabel 

然后添加目标事件到viewDidLoad方法如下面

mySwitch.addTarget(self, action: #selector(ViewController.switchIsChanged(_:)), forControlEvents: UIControlEvents.ValueChanged) 

当开关翻转UIControlEventValueChanged事件被触发,并且stateChanged方法将被调用。

func switchIsChanged(mySwitch: UISwitch) { 
    if mySwitch.on { 
     switchState.text = "UISwitch is ON" 
    } else { 
     switchState.text = "UISwitch is OFF" 
    } 
} 

雨燕3.0

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/

1

简短的教程如果你想改变开关就可以使用

mySwitch.on = true 

将其设置为“在“ 或

mySwitch.off == false 

将其设置为“关”

或者,如果你想获得的价值,你可以使用

let myBool = mySwitch.on 

如果交换机上的价值驱动为真,否则为false 。

您也可以叫的valueChanged一个动作(获取或更新值每当切换值改变)

@IBAction func valueChanged(sender: AnyObject) { 
    // some code 
} 

为此,您可以通过选择双屏显示...查看并按住Ctrl键从你的开关拖动到视图控制器并选择操作,如果尚未选择,则更改值。

+1

如果你不明白这个问题,请不要回答 – wruckie

3

拖动和从对象放下UISwitch图书馆 -

@ IBOutlet-

@IBOutlet weak var myswitch: UISwitch! 

@ IBAction-

@IBAction func onAllAccessory(sender: UISwitch) { 

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

你的功能 -

func onCall(){ 
     print("On is calling") 
    } 
    func offCall(){ 
     print("Off is calling") 
    } 
1

在斯威夫特3,是机会。

if mySwitch.on 

if mySwitch.isOn 

myprint

3

为SWIFT 3

enter image description here

@IBAction func switchValueChanged(_ sender: UISwitch) { 
     print(sender.isOn) 
}