再次回到noob问题。
出于某种原因,我收到以下错误:
2017-07-25 14:29:00.589401 + 0200 Yiives [1416:534883] ***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是键值符合编码的关键UserEmailAdresLogin。'
现在我想以绿色方式自己看问题,所以请赐教。
它不会让我加载登录视图。那我怎么能实现呢?D
我运行以下代码:
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var BackgroundButton: UIButton!
@IBOutlet weak var UserEmailAdresInput: UITextField!
@IBOutlet weak var UserPasswordInput: UITextField!
@IBOutlet weak var UserLogin: UIButton!
@IBOutlet weak var UserForgotPassword: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func CloseLogin(_ sender: Any) {
dismiss(animated: true, completion: nil )
}
@IBAction func Login(_ sender: Any) {
let UserEmail = UserEmailAdresInput.text
let UserPassword = UserPasswordInput.text
let UserEmailStored = UserDefaults.standard.string(forKey: "UserEmail");
let UserPasswordStored = UserDefaults.standard.string(forKey: "UserPassword");
if(UserEmailStored == UserEmail){
if(UserPasswordStored == UserPassword){
//Login is succesfull
UserDefaults.standard.set(true, forKey: "UserLoggedIn")
UserDefaults.standard.synchronize();
}
}
}
}
If you need the source where the data is bein inserted:
import UIKit
class EntryViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var UserEmailAdresInput: UITextField!
@IBOutlet weak var UserPasswordInput: UITextField!
@IBOutlet weak var UserPasswordInputRepeated: UITextField!
@IBOutlet weak var UserSignUp: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.UserEmailAdresInput.delegate = self
self.UserPasswordInput.delegate = self
self.UserPasswordInputRepeated.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Hide Keyboard upon Touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
self.view.endEditing(true)
}
//Hide Keyboard upon Return Key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == UserEmailAdresInput{
UserEmailAdresInput.resignFirstResponder()
} else if textField == UserPasswordInput{
UserPasswordInput.resignFirstResponder()
} else {
UserPasswordInputRepeated.resignFirstResponder()
}
return true
}
@IBAction func SignUp(_ sender: Any) {
let UserEmail = UserEmailAdresInput.text;
let UserPassword = UserPasswordInput.text;
let UserPasswordRepeated = UserPasswordInputRepeated.text;
//Check if fields are filled in correctly
if(UserEmail?.isEmpty == true || UserPassword?.isEmpty == true || UserPasswordRepeated?.isEmpty == true){
displayAlertMessage(userMessage: "Alle velden moeten ingevuld worden");
return;
}
if UserEmail?.range(of: "@") == nil{
displayAlertMessage(userMessage: "Vul een legitiem emailadres in");
return;
}
if (UserPassword?.characters.count)! < 5{
displayAlertMessage(userMessage: "Wachtwoord moet langer zijn dan 5 karakters");
}
if(UserPassword != UserPasswordRepeated){
displayAlertMessage(userMessage: "Wachtwoorden zijn niet gelijk");
return;
}
//Store Data
UserDefaults.standard.set(UserEmail, forKey: "UserEmail");
UserDefaults.standard.set(UserPassword, forKey: "UserPassword");
UserDefaults.standard.synchronize();
//SignUp Succesfull
var Alert = UIAlertController(title:"Succesvol aangemeld!", message: "Ga naar je email inbox om je aanmelding te voltooien", preferredStyle: UIAlertControllerStyle.alert);
let OkAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: nil);
Alert.addAction(OkAction);
self.present(Alert, animated: true, completion: nil)
}
func displayAlertMessage(userMessage:String){
var Alert = UIAlertController(title:"Melding", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);
let OkAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default, handler: nil);
Alert.addAction(OkAction);
self.present(Alert, animated: true, completion: nil)
}
}
答案 0 :(得分:1)
此错误表示您有一个名为UserEmailAdresLogin
的变量链接(我认为在您的情节提要文件中),但您的代码源中不再存在此变量。
要检查它,右键单击故事板中的控制器,它将显示链接列表,然后通过单击十字架进行删除。
如果问题不在故事板中,那就是变量名中的拼写错误(顺便说一句:“地址”)。