Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何仅当Swift中的状态更改为ON时从开关调用选择器?_Ios_Xcode_Swift - Fatal编程技术网

Ios 如何仅当Swift中的状态更改为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

当切换开关时,我需要使用选择器调用函数,但仅当我将开关设置为On状态时:

 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: