iOS 应用程序在尝试使用 UIPickerView 加载视图时崩溃 NSUnknownKeyException
Posted
技术标签:
【中文标题】iOS 应用程序在尝试使用 UIPickerView 加载视图时崩溃 NSUnknownKeyException【英文标题】:iOS Application Crashes NSUnknownKeyException When Trying to Load View with UIPickerView 【发布时间】:2015-08-02 01:21:46 【问题描述】:我有一个具有UIPickerView
的应用程序。第二次我尝试打开它所在的视图,应用程序崩溃并显示NSUnknownKeyException
。
这是日志:
2015-08-01 17:43:50.572 Patient Consent Project[1748:47245] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Patient_Consent_Project.ProcedurePicker 0x7ff381d1dd50> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key picker.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010de51c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f9bcbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010de518a9 -[NSException raise] + 9
3 Foundation 0x000000010e26fb53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010dd99d50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010e9c84eb -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010e8206d8 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010e820cc8 -[UIViewController loadView] + 109
8 UIKit 0x000000010e820f39 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010e8213ce -[UIViewController view] + 27
10 UIKit 0x000000010e73c289 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010e73c64f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010e748de1 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010e6ec417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x000000010e6ef19e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x000000010e6ee095 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x00000001114b95e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x000000010dd8541c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010dd7b165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010dd7af25 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010dd7a366 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010e6edb02 -[UIApplication _run] + 413
22 UIKit 0x000000010e6f08c0 UIApplicationMain + 1282
23 Patient Consent Project 0x000000010dc3fe97 main + 135
24 libdyld.dylib 0x00000001100f2145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这是我认为UIPickerView
的代码:
class ProcedurePicker: UIViewController, UIPickerViewDelegate
var selectedText = "Pick Procedure Here"
var procedures = ["Epidural Steroid Injection", "Selective Nerve Root Injection", "Facet Joint Injection", "Medial Branch Nerve Block", "Sympathetic Nerve Block", "Caudal Epidural Injection", "Intrathecal Opioid Injection", "Epidural Blood Patch", "Radiofrequency Ablation on Medial Branch Nerve", "Spinal Cord Stimulator Trial", "Piriformins Injection", "Tail Bone Injection", "Ganglion Impar Block", "Stellate Ganglion Block", "Discography"]
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
return 1
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return procedures.count
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
return procedures[row]
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
selectedText = procedures[row]
@IBAction func onPressDone(sender: UIButton)
ProcedureButtonText = selectedText
我对 Swift 和 ios 很陌生,所以您能否解释一下发生了什么以及为什么会发生这种崩溃?
【问题讨论】:
【参考方案1】:大多数时候,这是因为您在故事板中出口链接,而这个出口不再存在在视图控制器文件中。检查是否是这种情况(使用您的选择器)。
为此,打开您的故事板,点击该特定 UI(在您的情况下为选择器),检查它是否连接到某个插座(您可以如图所示检查它)。如果是,则使用十字符号将其删除。希望您的错误能够得到解决。如果您需要此插座,请将该方法添加到您的视图控制器文件中。
【讨论】:
【参考方案2】:这样做的通常原因是,在故事板的某个地方,您有一个指向代码中不再存在的插座的链接。在这种情况下,它似乎被称为picker
。
这可能是因为更改了代码而没有考虑从代码外部对它有哪些自动引用。
检查您的情节提要对象是否有不再有效的连接。
【讨论】:
以上是关于iOS 应用程序在尝试使用 UIPickerView 加载视图时崩溃 NSUnknownKeyException的主要内容,如果未能解决你的问题,请参考以下文章
iOS 应用程序在尝试使用 UIPickerView 加载视图时崩溃 NSUnknownKeyException
尝试在 iOS 13 或 tvOS 13 中使用 AVPictureInPictureController 时出错