我想创建一个笔尖并将其附加到我的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类。

viewDidLoadviewcontroller中,这就是我所说的VerificationCodeView ...

@IBOutlet weak var verificationCodeView1: VerificationCodeView! //OUTLET
fileprivate var loadingView: VerificationCodeView?


viewDidLoad中,

loadingView = VerificationCodeView(frame: verificationCodeView1.frame)


但是,当断点到达类Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil)中的行VerificationCodeView时,它将崩溃。

我在这里做错什么了...?

编辑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]


}

关于ios - 将 Nib 加载到 View 时崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54229518/

10-14 21:24