[英]Crash while loading a nib to a view
我想創建一個筆尖並將其附加到我的viewcontroller。 要創建筆尖,這就是我所做的...
首先,我添加了一個空白視圖並根據需要設計了該視圖。 然后我添加了一個快捷文件,並在其中添加了此代碼...
class VerificationCodeView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var mainLabel: UILabel!
fileprivate weak var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) //CRASHES HERE
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
然后在視圖控制器的情節提要中,添加了一個空白視圖,並使其成為VerificationCodeView
的類。
在viewcontroller
的viewDidLoad
中,這就是我所說的VerificationCodeView
。
@IBOutlet weak var verificationCodeView1: VerificationCodeView! //OUTLET
fileprivate var loadingView: VerificationCodeView?
在viewDidLoad
,
loadingView = VerificationCodeView(frame: verificationCodeView1.frame)
但是,當斷點到達類VerificationCodeView
的行Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil)
時,它將崩潰。
我在這里做錯什么了...?
編輯1:
這是顯示的整個錯誤日志...
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <…….> (loaded)' with name 'VerificationCodeView''
*** First throw call stack:
(
0 CoreFoundation 0x0000000109a261bb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000108fc4735 objc_exception_throw + 48
2 CoreFoundation 0x0000000109a26015 +[NSException raise:format:] + 197
3 UIKitCore 0x0000000112502d94 -[UINib instantiateWithOwner:options:] + 497
4 UIKitCore 0x0000000112503d17 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 214
5 MyApp 0x0000000106e01a1d $S24MyApp20VerificationCodeViewC10commonInit33_9E22480CE8C5AA6EA6A32BC6307198A8LLyyF + 205
6 MyApp 0x0000000106e0159f $S24MyApp20VerificationCodeViewC5coderACSgSo7NSCoderC_tcfc + 431
7 MyApp 0x0000000106e0162f $S24MyApp20VerificationCodeViewC5coderACSgSo7NSCoderC_tcfcTo + 47
8 UIKitCore 0x0000000112501166 -[UIClassSwapper initWithCoder:] + 246
9 UIFoundation 0x00000001101e55ad UINibDecoderDecodeObjectForValue + 749
10 UIFoundation 0x00000001101e52b3 -[UINibDecoder decodeObjectForKey:] + 251
11 UIKitCore 0x00000001125057b8 -[UIRuntimeConnection initWithCoder:] + 178
12 UIFoundation 0x00000001101e55ad UINibDecoderDecodeObjectForValue + 749
13 UIFoundation 0x00000001101e5854 UINibDecoderDecodeObjectForValue + 1428
14 UIFoundation 0x00000001101e52b3 -[UINibDecoder decodeObjectForKey:] + 251
15 UIKitCore 0x0000000112503067 -[UINib instantiateWithOwner:options:] + 1220
16 UIKitCore 0x0000000112276452 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
17 UIKitCore 0x0000000112276ddc -[UIViewController loadView] + 177
18 UIKitCore 0x00000001122770ee -[UIViewController loadViewIfRequired] + 175
19 UIKitCore 0x0000000112277940 -[UIViewController view] + 27
20 MyApp 0x0000000106dcd12a $S24MyApp19StepsViewControllerC20initialConfiguration33_8FADDC2BAB07508181BB037CD30FE9BDLLyyF + 2122
21 MyApp 0x0000000106dcc841 $S24MyApp19StepsViewControllerC11viewDidLoadyyF + 97
22 MyApp 0x0000000106dcc8c4 $S24MyApp19StepsViewControllerC11viewDidLoadyyFTo + 36
23 UIKitCore 0x00000001122774e1 -[UIViewController loadViewIfRequired] + 1186
24 UIKitCore 0x0000000112277940 -[UIViewController view] + 27
25 UIKitCore 0x0000000112a36775 __67-[UIStoryboardEmbedSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 180
26 UIKitCore 0x0000000112a387fb -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 276
27 UIKitCore 0x0000000112a386b9 -[UIStoryboardSegueTemplate _perform:] + 82
28 UIKitCore 0x0000000112a3897b -[UIStoryboardSegueTemplate perform:] + 157
29 UIKitCore 0x00000001122772cd -[UIViewController loadViewIfRequired] + 654
30 UIKitCore 0x0000000112277940 -[UIViewController view] + 27
31 UIKitCore 0x00000001121c1a9b -[UINavigationController _startCustomTransition:] + 931
32 UIKitCore 0x00000001121d83f0 -[UINavigationController _startDeferredTransitionIfNeeded:] + 741
33 UIKitCore 0x00000001121d97e0 -[UINavigationController __viewWillLayoutSubviews] + 150
34 UIKitCore 0x00000001121b9600 -[UILayoutContainerView layoutSubviews] + 217
35 UIKitCore 0x0000000112d80795 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1441
36 QuartzCore 0x0000000108ab9b19 -[CALayer layoutSublayers] + 175
37 QuartzCore 0x0000000108abe9d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
38 QuartzCore 0x0000000108a377ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
39 QuartzCore 0x0000000108a6e97e _ZN2CA11Transaction6commitEv + 576
40 UIKitCore 0x0000000112890701 _UIApplicationFlushRunLoopCATransactionIfTooLate + 165
41 UIKitCore 0x000000011298a569 __handleEventQueueInternal + 6874
42 CoreFoundation 0x000000010998b721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
43 CoreFoundation 0x000000010998b054 __CFRunLoopDoSources0 + 436
44 CoreFoundation 0x000000010998563f __CFRunLoopRun + 1263
45 CoreFoundation 0x0000000109984e11 CFRunLoopRunSpecific + 625
46 GraphicsServices 0x000000010fea91dd GSEventRunModal + 62
47 UIKitCore 0x000000011289681d UIApplicationMain + 140
48 MyApp 0x0000000106df9cb7 main + 71
49 libdyld.dylib 0x000000010b6de575 start + 1
50 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
請更新您的功能
private func commonInit() {
let uvNub: UIView = Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) //CRASHES HERE
self.addSubview(uvNub)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
試試下面的代碼,
fileprivate func commonInit(){
let bundle = Bundle(for: type(of: self))
bundle.loadNibNamed("VerificationCodeView", owner: self, options: nil)
self.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
containerView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
我發現您的代碼有問題。
您有一個contentView
的出口。 這意味着該視圖已經添加到您的視圖層次結構中。 然后在你的commonInit()
'addSubview(contentView)'中做錯了。
Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil)
的結果未使用。
您可以使用它從筆尖加載視圖:
let verificationCodeView = UINib(nibName: "VerificationCodeView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VerificationCodeView
如果要獲取VerificationCodeView
的實例,請編寫如下的類方法。
class func instantiateFromNib() -> VerificationCodeView {
let verificationCodeView = UINib(nibName: String(describing:self), bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VerificationCodeView
return verificationCodeView
}
現在,只要您想要此視圖,就可以像下面這樣獲得它:
let verificationCodeView = VerificationCodeView.instantiateFromNib()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.