Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
在swift for iOs中,我有一个if/else代码块,用于响应UISwitch中的更改。在某些情况下,如何将uiswitch设置回off?_Ios_Swift_If Statement_Cocoa Touch - Fatal编程技术网

在swift for iOs中,我有一个if/else代码块,用于响应UISwitch中的更改。在某些情况下,如何将uiswitch设置回off?

在swift for iOs中,我有一个if/else代码块,用于响应UISwitch中的更改。在某些情况下,如何将uiswitch设置回off?,ios,swift,if-statement,cocoa-touch,Ios,Swift,If Statement,Cocoa Touch,在我的swift iOS应用程序中,我有一个简单的UISwitch控件。我已将值更改插座连接到我的@iAction。代码如下所示: @IBAction func userDidSelectVisibiltySwitch(_ sender: Any) { if self.visibilitySwitch.isOn { if badCondition { self.visibilitySwith.setOn(false, animated: false)

在我的swift iOS应用程序中,我有一个简单的UISwitch控件。我已将值更改插座连接到我的@iAction。代码如下所示:

@IBAction func userDidSelectVisibiltySwitch(_ sender: Any) {

    if self.visibilitySwitch.isOn {
       if badCondition {
           self.visibilitySwith.setOn(false, animated: false)

          return
       }
     } else { // Strangely, it executes the else (I think because the compiler is evaluating the isOn condition again when it arrives to the else {}
        // work to be done if the user has turned off the switch
     }
}
我怀疑在这种情况下,当我在计算else之前关闭开关时,编译器会执行else{}语句,因为它会再次计算上面的isOn表达式。但考虑到我给出了“返回”指令,这怎么可能呢?我真的无法理解。对我的嫌疑犯的确认来自这样一个事实:如果我使用GCD分派_async“self.visibilitySwith.setOn(false,animated:false)”语句,它将在不执行else{}语句的情况下正常工作,因为对else的求值发生在我的语句关闭控件之前。我的代码现在看起来是这样的,它可以工作:

@IBAction func userDidSelectVisibiltySwitch(_ sender: Any) {

    if self.visibilitySwitch.isOn {
       if badCondition {
            DispatchQueue.main.async {
               self.visibilitySwith.setOn(false, animated: false)
            }
            return
       }
     } else { // In this case it is normal, it does not execute the else {}
        // work to be done if the user has turned off the switch
     }
}

我想我在这件事上遗漏了斯威夫特的一些重要内容。非常感谢您的帮助。我已经提供了一个解决方案,但我想了解这个问题。非常感谢

您没有通过
sender
参数访问
ui开关
,而是直接转到我假设的
IBOutlet
值。除此之外,您还可以访问发件人,如下所述:

@IBAction func userDidSelectVisibiltySwitch(_ sender: UISwitch) {
    if sender.isOn && badCondition {
        sender.setOn(false, animated: false)
    } else { // In this case it is normal, it does not execute the else {}
        // work to be done if the user has turned off the switch
    }
}
您的修复程序正常工作的原因可能是由于调度调用引入了轻微延迟,这允许
IBOutlet
值更新其值

我还将您的
if
语句组合在一起,因为您提供的示例不需要嵌套检查

根据RMADDY的评论进行更新

这个解决方案让我有一点代码的味道,经过进一步的调查,我能够重现OP描述的场景。这是通过在故事板中设置动作来实现的,如下所示:

通过这种设置,我看到了以下内容:

  • OP发布的原始代码将失败
  • 如OP所示,添加DispatchQueue将在短暂延迟后纠正切换
  • 我发布的解决方案将正常工作

假设这是OP所做的,那么第一个更正将是将事件更改为
Value Changed
。然后,正如rmaddy在注释中所述,无论您使用的是参数还是
IBOutlet
,这都将成功。根据最初的问题,我的解释是,接口中的出口值和开关状态不同步

调用
setOn(uquo:animated:)
函数不应触发iAction。此更改不应影响OP的原始代码。无论是通过
发送器
还是通过插座访问开关,都没有区别。这是指向同一个对象的指针。@rmaddy我完全同意,在你的评论之后,我进一步调查了一下,看是否可以进入OP描述的确切场景。我可以通过将操作设置为开关上的主操作触发器来做到这一点。这样做,我看到它在没有OP的调度下失败了,调度成功了,我的帖子成功了。基于此,我将用这些信息更新答案。