Ios 如何仅当Swift中的状态更改为ON时从开关调用选择器?
当切换开关时,我需要使用选择器调用函数,但仅当我将开关设置为On状态时:Ios 如何仅当Swift中的状态更改为ON时从开关调用选择器?,ios,xcode,swift,Ios,Xcode,Swift,当切换开关时,我需要使用选择器调用函数,但仅当我将开关设置为On状态时: cell.switchMy.addTarget(self, action: "openView", forControlEvents: UIControlEvents.ValueChanged) func openView(){ var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell5") as CustomC
cell.switchMy.addTarget(self, action: "openView", forControlEvents: UIControlEvents.ValueChanged)
func openView(){
var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell5") as CustomCell
if(cell.switchMy.on == true){
println("ON")
self.performSegueWithIdentifier("segueUnl", sender: self)
}
}
如果我使用这段代码,总是调用函数,但我正在控制开关状态是否为ON。。。这怎么可能正确呢?这是(几乎)正确的方法。只有当开关状态为on
时,才能调用方法。
但是您不需要从openView
方法中检索单元格。调用方将传递发件人:
cell.switchMy.addTarget(self, action: "openView:", forControlEvents: UIControlEvents.ValueChanged)
func openView(sender: UISwitch){
if(sender.on) {
println("ON")
self.performSegueWithIdentifier("segueUnl", sender: self)
}
}
小心:需要将addTarget
中使用的选择器从openView
更改为openView: