我有 10 个已连接的 UIButton,在 buttonPressed IBAction 中具有不同的标签,并且我的 segue 的 ID 为“GoToDetailVC”
import UIKit
class MainVC: UIViewController {
var buttonNumber: Int?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func buttonPressed(sender: AnyObject) {
buttonNumber = sender.tag
performSegue(withIdentifier: "GoToDetailVC", sender: sender.tag)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailVC {
if let btnNumber = sender as? Int {
destination.buttonNumber = btnNumber
}
}
}
}
然后我想用标签中的数字替换 headerLabel 文本
import UIKit
class DetailVC: UIViewController {
@IBOutlet weak var headerLabel: UILabel!
private var _buttonNumber: Int!
var buttonNumber: Int {
get {
return _buttonNumber
} set {
_buttonNumber = newValue
}
}
override func viewDidLoad() {
super.viewDidLoad()
headerLabel.text = "\(_buttonNumber!)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func dismissView(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
它崩溃并产生此消息
2016-12-31 12:36:36.439 Core Geometry[2638:539019] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Core_Geometry.DetailVC 0x7fc55251aea0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108a67d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010615b21e objc_exception_throw + 48
2 CoreFoundation 0x0000000108a67c99 -[NSException raise] + 9
3 Foundation 0x0000000105c699df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291
4 UIKit 0x00000001067be293 -[UIViewController setValue:forKey:] + 88
5 UIKit 0x0000000106a3279e -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x0000000108a0c9e0 -[NSArray makeObjectsPerformSelector:] + 256
7 UIKit 0x0000000106a31122 -[UINib instantiateWithOwner:options:] + 1867
8 UIKit 0x00000001067c49c5 -[UIViewController _loadViewFromNibNamed:bundle:] + 386
9 UIKit 0x00000001067c52e7 -[UIViewController loadView] + 177
10 UIKit 0x00000001067c561c -[UIViewController loadViewIfRequired] + 201
11 UIKit 0x00000001067c5e70 -[UIViewController view] + 27
12 UIKit 0x00000001070866a4 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87
13 UIKit 0x00000001067a0702 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 141
14 UIKit 0x00000001067d8e97 -[UIViewController _presentViewController:withAnimationController:completion:] + 3956
15 UIKit 0x00000001067dc26b -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530
16 UIKit 0x00000001067dbd51 -[UIViewController presentViewController:animated:completion:] + 179
17 UIKit 0x0000000106de9717 __74-[UIStoryboardPresentationSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 133
18 UIKit 0x0000000106df91dd -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 447
19 UIKit 0x0000000106df8fed -[UIStoryboardSegueTemplate _perform:] + 82
20 UIKit 0x00000001067c8a1f -[UIViewController performSegueWithIdentifier:sender:] + 99
21 Core Geometry 0x00000001056c6de1 _TFC13Core_Geometry6MainVC13buttonPressedfT6senderPs9AnyObject__T_ + 465
22 Core Geometry 0x00000001056c6f86 _TToFC13Core_Geometry6MainVC13buttonPressedfT6senderPs9AnyObject__T_ + 54
23 UIKit 0x00000001066258bc -[UIApplication sendAction:to:from:forEvent:] + 83
24 UIKit 0x00000001067abc38 -[UIControl sendAction:to:forEvent:] + 67
25 UIKit 0x00000001067abf51 -[UIControl _sendActionsForEvents:withEvent:] + 444
26 UIKit 0x00000001067aae4d -[UIControl touchesEnded:withEvent:] + 668
27 UIKit 0x0000000106693545 -[UIWindow _sendTouchesForEvent:] + 2747
28 UIKit 0x0000000106694c33 -[UIWindow sendEvent:] + 4011
29 UIKit 0x00000001066419ab -[UIApplication sendEvent:] + 371
30 UIKit 0x0000000106e2e72d __dispatchPreprocessedEventFromEventQueue + 3248
31 UIKit 0x0000000106e27463 __handleEventQueue + 4879
32 CoreFoundation 0x0000000108a0c761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
33 CoreFoundation 0x00000001089f198c __CFRunLoopDoSources0 + 556
34 CoreFoundation 0x00000001089f0e76 __CFRunLoopRun + 918
35 CoreFoundation 0x00000001089f0884 CFRunLoopRunSpecific + 420
36 GraphicsServices 0x000000010a965a6f GSEventRunModal + 161
37 UIKit 0x0000000106623c68 UIApplicationMain + 159
38 Core Geometry 0x00000001056c654f main + 111
39 libdyld.dylib 0x0000000105b8768d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
显然你的 nib 文件有问题。尝试首先删除
tableView
和其他视图之间的所有连接。这意味着所有出口 (IBOutlet
) 和行动 (IBAction
)。崩溃很可能会消失。之后,您可以尝试重新连接插座和操作,但请确保操作正确。因为错误表明您正在使用 tableView
与类建立连接,但它没有名为 tableView
的键(属性)。因此,您可能会尝试与错误的类/对象建立连接。
我们看到类似的崩溃,在 30 万次会话中总共有 12 个会话,其中 80% 肯定在发生崩溃的地方使用了此功能。自从我们看到这种情况发生以来,它对我们的测试来说效果很好。以前从未遇到过任何崩溃。不知道为什么会这样。
我们在tableview didSelectRowAt中调用performSegue。
请查看下面的崩溃 -
0 CoreFoundation 0x18d604f20 __exceptionPreprocess + 164 (NSException.m:249)
1 libobjc.A.dylib 0x18548f2b8 objc_exception_throw + 60 (objc-exception.mm:356)
2 UIKitCore 0x190814604 __66-[UIStoryboardPushSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 880 (UIStoryboardPushSegueTemplate.m:58)
3 UIKitCore 0x190818dbc -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 172 (UIStoryboardSegueTemplate.m:134)
4 UIKitCore 0x190818cdc -[UIStoryboardSegueTemplate _perform:] + 68 (UIStoryboardSegueTemplate.m:121)
5 UIKitCore 0x1900e658c -[UIViewController performSegueWithIdentifier:sender:] + 300 (UIViewController.m:5598)
6 OurAppName 0x102154a78 ViewController.tableView(_:didSelectRowAt:) + 568 (ViewController.swift:0)
7 OurAppName 0x102154c38 @objc ViewController.tableView(_:didSelectRowAt:) + 136 (<compiler-generated>:0)
8 UIKitCore 0x18fab7904 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:deselectPrevious:performCustomSelectionAction:] + 1232 (UITableView.m:9323)
9 UIKitCore 0x190876af4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:animatedSelection:] + 268 (UITableView.m:9377)
10 UIKitCore 0x190876bf4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 216 (UITableView.m:9396)
11 UIKitCore 0x18f8bd5d8 -[_UIAfterCACommitBlock run] + 72 (_UIAfterCACommitQueue.m:137)
12 UIKitCore 0x18f8bd49c -[_UIAfterCACommitQueue flush] + 164 (_UIAfterCACommitQueue.m:228)
13 UIKitCore 0x18f8bd3b4 _runAfterCACommitDeferredBlocks + 496 (UIApplication.m:3144)
14 UIKitCore 0x18f8bcfec _cleanUpAfterCAFlushAndRunDeferredBlocks + 80 (UIApplication.m:3108)
15 UIKitCore 0x18f8bcefc _UIApplicationFlushCATransaction + 72 (UIApplication.m:3185)
16 UIKitCore 0x18f8ba660 _UIUpdateSequenceRun + 84 (_UIUpdateSequence.mm:119)
17 UIKitCore 0x18f8ba2a4 schedulerStepScheduledMainSection + 172 (_UIUpdateScheduler.m:1058)
18 UIKitCore 0x18f8bb148 runloopSourceCallback + 92 (_UIUpdateScheduler.m:1221)
19 CoreFoundation 0x18d5d7834 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1957)
20 CoreFoundation 0x18d5d77c8 __CFRunLoopDoSource0 + 176 (CFRunLoop.c:2001)
21 CoreFoundation 0x18d5d5298 __CFRunLoopDoSources0 + 244 (CFRunLoop.c:2038)
22 CoreFoundation 0x18d5d4484 __CFRunLoopRun + 828 (CFRunLoop.c:2955)
23 CoreFoundation 0x18d5d3cd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
24 GraphicsServices 0x1d20211a8 GSEventRunModal + 164 (GSEvent.c:2196)
25 UIKitCore 0x18fc0dae8 -[UIApplication _run] + 888 (UIApplication.m:3713)
26 UIKitCore 0x18fcc1d98 UIApplicationMain + 340 (UIApplication.m:5303)
27 OurAppName. 0x10212b228 main + 64 (AppDelegate.swift:21)
28 dyld 0x1b0dab154 start + 2356 (dyldMain.cpp:1298)