我尝试在我的 Main.Storyboard 上添加我的导出,然后 CTRL + 将它拖到我相应的 View Controller 中。
class LoginViewController: UIViewController {
@IBOutlet weak var nameField: UITextField!
}
执行此操作时,出现此错误
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7f7ff051e860> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameField.'
可能导致此问题的原因是什么?
最佳答案
这里的问题是它正在创建 UIViewController
的实例, 不是 LoginViewController
的实例.您可以判断,因为错误消息显示 <UIViewController 0x7f7ff051e860>
, 不是 <LoginViewController 0x7f7ff051e860>
.自 UIViewController
没有 nameField
socket ,当 Storyboard 加载程序尝试连接该 socket 时,您会收到错误消息。
有两种可能的解释:
Storyboard中的自定义类未设置为
LoginViewController
.在 Storyboard的场景中选择 View Controller 。从菜单栏中,选择“ View ”>“实用程序”>“显示标识检查器”。然后,在身份检查器(右侧 Pane )中,确保将类设置为LoginViewController
.如果 Xcode 让您连接 socket ,您必须在某个时候设置自定义类。但是您之后可能不小心撤消了更改。
包含
LoginViewController
的文件源代码不是您的目标(您的应用程序)的一部分。在这种情况下,您的调试控制台中应该有另一条消息,如下所示:2016-02-08 22:08:04.569 test[3049:9594499] Unknown class _TtC4test16MyViewController in Interface Builder file.
要解决此问题,请打开您的
LoginViewController.swift
文件(或任何包含该类的文件)。从菜单栏中选择“ View ”>“实用程序”>“显示文件检查器”。在文件检查器(右侧 Pane )中,找到目标成员资格部分并选中您的应用程序旁边的复选框。
关于ios - 无法为 UITextField 链接 IBOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283610/