ios - 将 Nib 加载到 View 时崩溃

标签 ios swift

我想创建一个 Nib 并将其附加到我的 View Controller 。为了创建 Nib ,这就是我所做的......

首先,我添加了一个空白 View 并根据需要设计了该 View 。然后我添加了一个 swift 文件并在其中添加了这段代码...

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]
    }
}

然后在 View Controller 的 Storyboard中,我添加了一个空白 View ,并将其设为 VerificationCodeView 类。

viewcontrollerviewDidLoad中,这就是我调用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]

}

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

相关文章:

iphone - 设置状态栏方向在 iPad 2 的 iOS 6 中不起作用

ios - 如何在 React Native 0.60+ 中添加原生模块?

android - 计算软件键盘高度 - iOS 和 Android

objective-c - 当应用程序进入后台时如何在自定义应用程序中播放 iPod 中的音乐

swift - RLMException',原因 : 'Attempting to modify object outside of a write transaction

ios - SecPKCS12Import when p12 requires an empty passphrase

json - 当我遍历 "Type ' 结果时,错误 'Sequence"Any' does not conform to protocol 'JSON' 是什么意思?

swift - 使用 Swift,为什么按下一个 UIStepper 会影响两个步进器?

swift - Swift 中的“advancedBy”不可用

ios - Swift 如何让一个函数在另一个函数完成后执行