【问题标题】:Loading isOn or isOff for a UISwitch using Swift 4?使用 Swift 4 为 UISwitch 加载 isOn 或 isOff?
【发布时间】:2018-02-05 13:33:34
【问题描述】:

我正在开发一个项目,我需要使用 Swift 4 将开关加载为 .isOn 或 .isOff。我可以在项目的主情节提要中通过将开关设置为打开或关闭来执行此操作,但我希望根据我数据库中每个用户的布尔值打开或关闭开关。

    //Create outlet for UISwitch
    @IBOutlet weak var switchBool1Outlet: UISwitch!

viewDidLoad() {
    //pulling data from Google Firebase to get true or false value    
    dataRef.reference().child("USERS/\(uid!)").observe(.value, with: { snapshot in
                    if let dictionary = snapshot.value as? [String: Any]
                    {
                        //MARK - Local Variables
                        var bool1Value = dictionary["bool1"] as! Bool

                        //trying to set switches here, this is not working
                        if bool1Value == true && bool1Value != nil {
                          //following throws error: "Expression resolves to an unused l-value"
                          self.switchBool1Outlet.isOn
                        }
        })
}

关于如何根据数据库中的真/假值让 UISwitch 加载/关闭有什么想法吗?

【问题讨论】:

  • 你没有指定mealPlan是什么。
  • @M.Wojcik 为了这个例子,打算改变那个 bool1Value。我的应用程序与我大学的用餐分享有关。欣赏评论!
  • 谢谢,@Adrian!该链接非常有帮助,并帮助我回答了其他几个问题。我很感激!

标签: swift xcode uiswitch


【解决方案1】:

使用以下代码改变开关状态,见here

self.switchBool1Outlet.setOn(true, 
  animated: true)

【讨论】:

  • 谢谢@Fahadsk!这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2019-02-08
相关资源
最近更新 更多