2017-05-21 11 views

答えて

2

これが私のために私は

loginTxt - username textfield 
passwordTxt - password textfield 
rememberSwitch - switch 

コード

func viewDidLoad() { 

     rememberSwitch.addTarget(self, action: #selector(self.stateChanged), for: .valueChanged) 
     let defaults: UserDefaults? = UserDefaults.standard 

// check if defaults already saved the details 

     if defaults?.bool(forKey: "ISRemember") { 
      loginTxt.text = defaults?.value(forKey: "SavedUserName") 
      passwordTxt.text = defaults?.value(forKey: "SavedPassword") 
      rememberSwitch.setOn(true, animated: false) 
     } 
     else { 
      rememberSwitch.setOn(false, animated: false) 
     } 
    } 

    func stateChanged(_ switchState: UISwitch) { 

     let defaults: UserDefaults? = UserDefaults.standard 
     if switchState.isOn() { 
      defaults?.set(true, forKey: "ISRemember") 
      defaults?.set(loginTxt.text, forKey: "SavedUserName") 
      defaults?.set(passwordTxt.text, forKey: "SavedPassword") 
     } 
     else { 
      defaults?.set(false, forKey: "ISRemember") 
      } 
      } 
を接続

後コンセントを働い

関連する問題