提醒复选框未选中第一次在swift中

时间:2017-08-28 12:57:19

标签: ios swift checkbox

enter image description here

我是Swift的新用户我可以在登录页面中使用提醒复选框正常工作。

  1. 当提醒复选框选择在日志文本字段中显示凭据时,它正在工作
  2. 当提醒checkBox取消选择凭据时,日志文本字段正在运行
  3. 但是当注销之后如果我可以单击提醒复选框以取消选中提醒复选框,那么当我单击双倍选择时,它不会选择第一次。

    这是代码

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
    
          let checkviewapper = UserDefaults.standard
          let logindata = UserDefaults.standard
          let data = checkviewapper.integer(forKey:"checked")
    
            print("data values is ",data)
            if(data == 1)
            {
    
              RmdButton.setImage( UIImage(named:"check.png"), for: .normal)
              emailText.text = logindata.string(forKey: "emaild_default")
              passwordText.text = logindata.string(forKey: "password_default")
    
            }
            else
            {
    
                emailText.text = ""
                passwordText.text = ""
                RmdButton.setImage( UIImage(named:"uncheck.jpeg"), for: .normal)
    
            }
        }
    
    
        @IBAction func checkBox(_ sender: UIButton) {
    
    
            let defautls = UserDefaults.standard
            defautls.removeObject(forKey:"checked")
    
            defautls.synchronize()
    
           if unchecked {
    
    
                 let defautls = UserDefaults.standard
                 defautls.set(unchecked, forKey: "checked")
                 defautls.synchronize()
    
                if let myLoadedString = defautls.string(forKey: "checked") {
    
                }
    
                sender.setImage( UIImage(named:"check.png"), for: .normal)
                print("checked inside")
                unchecked = false
            }
            else {
                sender.setImage( UIImage(named:"uncheck.jpeg"), for: .normal)
                print("uncheckinside")
                unchecked = true
            }
    
        }
    

    我错误的地方,如何在第一时间取消选中提醒CheckBox?

1 个答案:

答案 0 :(得分:1)

试试这个: -

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)


    let defautls = UserDefaults.standard

    if defautls.bool(forKey: "checked") == true {
    {
        RmdButton.setImage( UIImage(named:"check.png"), for: .normal)
        emailText.text = logindata.string(forKey: "emaild_default")
        passwordText.text = logindata.string(forKey: "password_default")

    }
    else
    {
        emailText.text = ""
        passwordText.text = ""
        RmdButton.setImage( UIImage(named:"uncheck.jpeg"), for: .normal)

    }
}
}

@IBAction func action(_ sender: UIButton) {

        let defautls = UserDefaults.standard

        if defautls.bool(forKey: "checked") == false {
            defautls.set(true, forKey: "checked")
            sender.setImage( UIImage(named:"check.png"), for: .normal)
            print("checked inside")
            unchecked = false
        }
        else {
            defautls.set(false, forKey: "checked")
            sender.setImage( UIImage(named:"uncheck.jpeg"), for: .normal)
            print("uncheckinside")
            unchecked = true
        }

}

当您点击退出时设置默认值: -

            let defautls = UserDefaults.standard
            defautls.set(false, forKey: "checked")