【问题标题】:Crash while loading a nib to a view将笔尖加载到视图时崩溃
【发布时间】:2019-01-17 05:25:09
【问题描述】:

我想创建一个笔尖并将其附加到我的视图控制器。为了创建笔尖,这就是我所做的......

首先我添加了一个空白视图并根据需要设计了该视图。然后我添加了一个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]
    }
}

然后在视图控制器中,在故事板中,我添加了一个空白视图并将其设为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

【问题讨论】:

  • 崩溃时控制台中显示的错误的确切文本是什么?
  • 它说 THREAD_BAD ACCESS
  • 您能否将错误消息的整个文本粘贴到您的问题中? (我的意思是所有内容,包括发生错误访问的十六进制地址。)
  • 好的@user1118321...等一下..
  • 它可能是由于死锁而导致的崩溃从所需的 init?(coder aDecoder:NSCoder)它调用 commonInit 并从 commonInit init?(coder aDecoder:NSCoder)调用由于 loadNibNamed。请在从require init中删除commonInit后检查它

标签: ios swift


【解决方案1】:

请更新您的功能

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]

}

【讨论】:

  • 我试过@Parth Dhorda ...但在这一行let uvNub: UIView = Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) 它显示错误Cannot convert value of type '[Any]?' to specified type 'UIView'
【解决方案2】:

试试下面的代码,

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
}

【讨论】:

    【解决方案3】:

    我发现你的代码有问题。

    1. 您有一个contentView 的出口。这意味着该视图已添加到您的视图层次结构中。然后在你的commonInit() 'addSubview(contentView)' 中做错了。

    2. Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) 的结果未被使用。

    您可以使用它从 nib 加载视图:

    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()
    

    【讨论】:

    • 你的意思是 commonInit 方法...?
    • 在“commonInit”中是的
    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 2013-06-04
    • 2019-06-09
    • 2015-10-19
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多