报错:'NSUnknownKeyException', reason: TestViewController 0xxxxxx setValue:forUndefinedKey

调用语句:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TestViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"TestViewController"];
[self.navigationController presentViewController:viewController animated:YES completion:NULL];

报错如下:

2018-09-05 10:30:40.280978+0800 Test[85948:5715317] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TestViewController 0x7fd52363b230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testShowIv.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000110e131e6 __exceptionPreprocess + 294
    1   libobjc.A.dylib                     0x000000010f4d3031 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000110e130b9 -[NSException raise] + 9
    3   Foundation                          0x000000010eef4b47 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x0000000112b3eec0 -[UIViewController setValue:forKey:] + 87
    5   UIKit                               0x0000000112e2ce8a -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x0000000110db5e8d -[NSArray makeObjectsPerformSelector:] + 317
    7   UIKit                               0x0000000112e2b834 -[UINib instantiateWithOwner:options:] + 1856
    8   UIKit                               0x0000000112b460d7 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
    9   UIKit                               0x0000000112b46a04 -[UIViewController loadView] + 177
    10  UIKit                               0x0000000112b46d21 -[UIViewController loadViewIfRequired] + 175
    11  UIKit                               0x0000000112b47574 -[UIViewController view] + 27
    12  UIKit                               0x00000001135d2a43 -[_UIFullscreenPresentationController _setPresentedViewController:] + 89
    13  UIKit                               0x0000000112b13f17 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133
    14  UIKit                               0x0000000112b5a4d0 -[UIViewController _presentViewController:withAnimationController:completion:] + 3782
    15  UIKit                               0x0000000112b5d2c6 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 99
    16  UIKit                               0x0000000112b5d958 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 511
    17  UIKit                               0x0000000112b5d22c -[UIViewController _presentViewController:animated:completion:] + 169
    18  UIKit                               0x0000000112b5d570 -[UIViewController presentViewController:animated:completion:] + 154
    19  Test                       0x000000010d7f9fab -[AppDelegate showTestVc] + 539
    20  Test                       0x000000010d7f9c2c -[AppDelegate applicationWillEnterForeground:] + 1020
    21  UIKit                               0x00000001129bb91d -[UIApplication _sendWillEnterForegroundCallbacks] + 75
    22  UIKit                               0x0000000112d646b4 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1404
    23  UIKit                               0x00000001136d5614 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 299
    24  UIKit                               0x00000001136d54ae -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    25  UIKit                               0x00000001133b975d __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    26  UIKit                               0x00000001135b44b7 _performActionsWithDelayForTransitionContext + 100
    27  UIKit                               0x00000001133b9627 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    28  UIKit                               0x00000001131360e0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    29  UIKit                               0x0000000112f730bd -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 502
    30  FrontBoardServices                  0x000000011d693d2d __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.362 + 198
    31  libdispatch.dylib                   0x0000000114c5d7ec _dispatch_client_callout + 8
    32  libdispatch.dylib                   0x0000000114c62db8 _dispatch_block_invoke_direct + 592
    33  FrontBoardServices                  0x000000011d6c7470 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    34  FrontBoardServices                  0x000000011d6c712e -[FBSSerialQueue _performNext] + 439
    35  FrontBoardServices                  0x000000011d6c768e -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    36  CoreFoundation                      0x0000000110db5bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    37  CoreFoundation                      0x0000000110d9a4af __CFRunLoopDoSources0 + 271
    38  CoreFoundation                      0x0000000110d99a6f __CFRunLoopRun + 1263
    39  CoreFoundation                      0x0000000110d9930b CFRunLoopRunSpecific + 635
    40  GraphicsServices                    0x0000000116ce0a73 GSEventRunModal + 62
    41  UIKit                               0x00000001129a3057 UIApplicationMain + 159
    42  Test                       0x000000010d82e04f main + 111
    43  libdyld.dylib                       0x0000000114cda955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

原因:

在解析对应storyboard的界面和ViewController的时候,出现了界面中的View,如按钮,输入框等已经设置了对应ViewController中的对象,但是却找不到。如按钮设置对应了btn,但是ViewController中并没有这个对象。如上面报错的:testShowIv,在界面中有这个指向(Referencing Outlets),但是当前设置的ViewController并没有这个对象。
出现这种问题主要有可能的情况:

1 界面是复制的,有对应之前ViewController的类,但是当前新的ViewController没有找到对应对象。
2 界面的Custom Class设置错误

解决方法:

只能按照报错找到到底是哪一个View出现了问题,然后该删除的删除(删除界面中View的(Referencing Outlets 或者 Sent Events)等指向),该添加的添加(在其对应ViewController中添加对象)。

猜你喜欢

转载自blog.csdn.net/lllkey/article/details/82417132