我正在使用Swift开发一个闪卡应用程序,我做得很糟糕。今天,我一直在做一些非常基本的事情,比如按下按钮在视图之间转换,但在遇到许多不同的错误并多次重新启动项目后,我再次陷入困境。
一切都正常,直到我决定将UITextField添加到我的一个视图中,现在每当我转换到那个视图时,它就会崩溃。但是,如果我从视图中删除UIText字段并取消连接,一切都会正常进行。我对自己做错了什么感到困惑,但我认为这可能与我一整天都在错误地看待这些观点和观点有关。
编辑:这是完整的错误消息
2020-12-01 12:42:41.206345-0800 final_proj690[3252:142523] [Storyboard] Unknown class HiraganaViewController in Interface Builder file.
2020-12-01 12:42:41.224722-0800 final_proj690[3252:142523] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7fa939d12790> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Question.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2041daf2 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff20177e78 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2041d793 -[NSException init] + 0
3 Foundation 0x00007fff207a5fca -[NSObject(NSKeyValueCoding) setValue:forKey:] + 325
4 UIKitCore 0x00007fff23f38cea -[UIViewController setValue:forKey:] + 87
5 UIKitCore 0x00007fff24279f7d -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x00007fff2040729c -[NSArray makeObjectsPerformSelector:] + 228
7 UIKitCore 0x00007fff2427612f -[UINib instantiateWithOwner:options:] + 2175
8 UIKitCore 0x00007fff23f40b48 -[UIViewController loadView] + 668
9 UIKitCore 0x00007fff23f40ee3 -[UIViewController loadViewIfRequired] + 172
10 UIKitCore 0x00007fff23f4165d -[UIViewController view] + 27
11 UIKitCore 0x00007fff23f5933f -[UIViewController _setPresentationController:] + 100
12 UIKitCore 0x00007fff23f51260 -[UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 1409
13 UIKitCore 0x00007fff23f54db3 -[UIViewController _presentViewController:withAnimationController:completion:] + 4942
14 UIKitCore 0x00007fff23f55bf6 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 98
15 UIKitCore 0x00007fff23f55f2c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 519
16 UIKitCore 0x00007fff23f55b54 -[UIViewController _presentViewController:animated:completion:] + 179
17 UIKitCore 0x00007fff23f55fe2 -[UIViewController presentViewController:animated:completion:] + 155
18 UIKitCore 0x00007fff247f6e05 __74-[UIStoryboardPresentationSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 133
19 UIKitCore 0x00007fff247fc71a -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 276
20 UIKitCore 0x00007fff247fc5d8 -[UIStoryboardSegueTemplate _perform:] + 82
21 UIKitCore 0x00007fff23f44523 -[UIViewController performSegueWithIdentifier:sender:] + 540
22 final_proj690 0x0000000103d46e66 $s13final_proj69014ViewControllerC14HiraganaButtonyySo8UIButtonCFyycfU_ + 118
23 final_proj690 0x0000000103d46f10 $sIeg_IeyB_TR + 48
24 libdispatch.dylib 0x0000000103fda9c8 _dispatch_client_callout + 8
25 libdispatch.dylib 0x0000000103fdd316 _dispatch_continuation_pop + 557
26 libdispatch.dylib 0x0000000103ff0e8b _dispatch_source_invoke + 2205
27 libdispatch.dylib 0x0000000103fe8ca4 _dispatch_main_queue_callback_4CF + 687
28 CoreFoundation 0x00007fff2038bdab __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
29 CoreFoundation 0x00007fff2038662e __CFRunLoopRun + 2685
30 CoreFoundation 0x00007fff203856c6 CFRunLoopRunSpecific + 567
31 GraphicsServices 0x00007fff2b76adb3 GSEventRunModal + 139
32 UIKitCore 0x00007fff24675187 -[UIApplication _run] + 912
33 UIKitCore 0x00007fff2467a038 UIApplicationMain + 101
34 libswiftUIKit.dylib 0x00007fff541545f2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
35 final_proj690 0x0000000103d4a8fa $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
36 final_proj690 0x0000000103d4a86e $s13final_proj69011AppDelegateC5$mainyyFZ + 46
37 final_proj690 0x0000000103d4a949 main + 41
38 libdyld.dylib 0x00007fff20256409 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7fa939d12790> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Question.'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.0.2 - Device: iPhone 12 (4D085E44-F6B4-4CFC-B984-C54CA04EB2AE) - Runtime: iOS 14.2 (18B79) - DeviceType: iPhone 12
这是我的代码:
主视图控制器代码:
class ViewController: UIViewController {
// create class variable that's accessible outside of viewToLoad()
var centerY: NSLayoutConstraint?
var top: NSLayoutConstraint?
var centerConstraintActive: Bool = true
@IBAction func HiraganaButton(_ sender: UIButton) {
let coloredView = ColoredView(width: 250, height: 50, color: .red)
let lb = UILabel(frame: CGRect(x: coloredView.frame.height/2, y: coloredView.frame.width/2, width: 50, height: 50))
lb.font = UIFont(name: "AmericanTypewriter", size: 30)
lb.translatesAutoresizingMaskIntoConstraints = false
lb.text=" Hiragana Time!"
view.addSubview(coloredView)
coloredView.addSubview(lb)
lb.center = coloredView.center
coloredView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
centerY = coloredView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
centerY?.isActive = true
top = coloredView.topAnchor.constraint(equalTo: view.topAnchor)
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.performSegue(withIdentifier: "HiraganaSegue", sender: self)
}
}
@IBAction func KatakanaButton(_ sender: UIButton) {
let coloredView = ColoredView(width: 250, height: 50, color: .systemTeal)
let lb = UILabel(frame: CGRect(x: coloredView.frame.height/2, y: coloredView.frame.width/2, width: 50, height: 50))
lb.font = UIFont(name: "AmericanTypewriter", size: 30)
lb.translatesAutoresizingMaskIntoConstraints = false
lb.text=" Katakana Time!"
view.addSubview(coloredView)
coloredView.addSubview(lb)
lb.center = coloredView.center
coloredView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
centerY = coloredView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
centerY?.isActive = true
top = coloredView.topAnchor.constraint(equalTo: view.topAnchor)
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.performSegue(withIdentifier: "KatakanaSegue", sender: self)
} }
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func coloredViewTapped() {
toggle()
}
func toggle() {
centerY?.isActive = false
top?.isActive = true
}
}
以下是在应用程序转换时导致应用程序崩溃的视图代码:
import UIKit
class HiraganaViewController: UIViewController {
@IBOutlet var Question: UIView!
var flashCards = ["a" : "あ",
"i" : "い",
"u" : "う"
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
这是ColoredView类的代码:
import UIKit
class ColoredView: UIView {
init(width: CGFloat, height: CGFloat, color: UIColor) {
super.init(frame: .zero)
// create the constraints (layout anchors)
self.widthAnchor.constraint(equalToConstant: width).isActive = true
self.heightAnchor.constraint(equalToConstant: height).isActive = true
// Turn off automatic "frame" constraints
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = color
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
再一次,我觉得这与我的连接有关,但当我查看连接列表时,我看到的只是我链接的东西。。。有什么想法吗?谢谢:(
崩溃日志的这一部分:
2020-12-01 12:42:41:206345-0800 final_proj690[3522:142523][Storyboard]接口生成器中的未知类HirakanaViewController文件
2020-12-01 12:42:41-2224722-0800最终_项目690[3522:142523]***由于未捕获的异常"NSUnknownKeyException"而终止应用程序,原因:'[<UIViewController 0x7fa939d12790>setValue:forUndefinedKey:]:此类不是键值编码符合关键问题。'
显示两个不同的问题:
问题1:
它说你引用了一个你没有定义的类HiraganaViewController
。
然而,我在您发布的源代码中看到了该类的类定义。打开";平假名ViewController.swift";文件,然后选择";文件检查器";。确保在您的";目标成员资格";部分,将该源文件包含到应用程序中。
问题2:
您有一个名为Question
的IBOutlet。这个视图看起来好像没有连接到它的出口。这可能是由第一个错误引起的。如果你修复了第一个错误,并且这种情况仍然发生:
进入情节提要并在其场景中选择HiraganaViewController
。然后选择连接检查器,查找Question
的条目,并单击其上的X以断开到IBOutlet的链接。然后打开第二个辅助编辑器,控制从Question对象拖动到IBOutlet
声明。