我目前正在关注设计iOS应用程序的系列文章。我到了需要在应用程序中添加另一个场景并连接诸如标签和文本字段之类的对象的地步。我创建了一个新的swift文件,并将其链接到故事板上的视图。问题是每次我按第一个视图上的按钮将应用程序崩溃时转到第二个视图,并且出现如下错误:
2018-10-03 21:24:46.905160-0400更改我的思维V2 [1669:53879] Interface Builder文件中的未知类SecondViewController。 2018-10-03 21:24:46.933760-0400更改我的想法V2 [1669:53879] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是键值与键NameTextField兼容。 * 第一个调用堆栈: libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)
我已经检查了接口生成器,它显示我的类文件确实是SecondViewController,具有正确的拼写和所有内容,该类的旁边甚至还有一个小箭头,将我带到直接类。我不知道为什么会这样,任何建议都会很棒。这是我的代码:
import UIKit
class SecondViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var NameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
NameTextField.delegate = self // Error on this line: Thread 1:
// Fatal error: Unexpectedly found
// nil while unwrapping an Optional
// value
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
连同图片一起显示所有内容,区分大小写是正确的,
对于为什么会出现此错误,我确实感到困惑,我什至尝试制作一个新项目并重新启动,但是当我移到第二个场景时仍然遇到相同的错误。任何建议都会有所帮助,谢谢!