gpt4 book ai didi

ios - 仅当在 Swift 中更改为 ON 状态时如何从开关调用选择器?

转载 作者:搜寻专家 更新时间:2023-11-01 06:48:29 25 4
gpt4 key购买 nike

我需要在改变开关时调用一个带有选择器的函数,但只有当我将开关设置为打开状态时:

 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:

关于ios - 仅当在 Swift 中更改为 ON 状态时如何从开关调用选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26523965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com