如何在iOS Swift中从xib UIViewController初始化?

问题描述 投票:0回答:1

[我一直在开发框架,我已经使用CarbonCarcocoapods使用选项卡菜单,现在我正尝试浏览UIVIewController xib。

当UITabBarController加载时,carbonkit菜单的默认选项卡菜单将被调用。

这里是用于加载xib UIViewcontroller的代码

 public func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {
    print("called carbonkit")       
    print("carbon kit index 0 called")

    return (UINib(nibName: "FirstViewController", bundle: Bundle(for: FirstViewController.self)).instantiate(withOwner: nil, options: nil).first as? FirstViewController)!

}

这是控制台输出:

 Called tab
   called carbonkit
  carbon kit index 0 called
 2019-11-12 22:13:02.350434+0530 A8FlowSampleApp[41059:1971714] *** 
 Terminating app due to uncaught exception 'NSUnknownKeyException', 
 reason: '[<NSObject 0x600000b1c8b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
 *** First throw call stack:
(
0   CoreFoundation                      0x00007fff23c4f02e __exceptionPreprocess + 350
1   libobjc.A.dylib                     0x00007fff50b97b20 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff23c4ebf9 -[NSException raise] + 9
3   Foundation                          0x00007fff256f2ef3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 325
4   UIKitCore                           0x00007fff474b9a52 -[UIRuntimeOutletConnection connect] + 109
5   CoreFoundation                      0x00007fff23c3b052 -[NSArray makeObjectsPerformSelector:] + 242
6   UIKitCore                           0x00007fff474b6bfa -[UINib instantiateWithOwner:options:] + 2190
7   A8Flow                              0x0000000104db51d8 $s6A8Flow6MyTaskC24carbonTabSwipeNavigation_16viewControllerAtSo06UIViewJ0CSo06CarbonfgH0C_SutF + 1016
8   A8Flow                              0x0000000104db546d $s6A8Flow6MyTaskC24carbonTabSwipeNavigation_16viewControllerAtSo06UIViewJ0CSo06CarbonfgH0C_SutFTo + 77
9   CarbonKit                           0x00000001048c1d31 -[CarbonTabSwipeNavigation viewControllerAtIndex:] + 593
10  CarbonKit                           0x00000001048c18fb -[CarbonTabSwipeNavigation loadFirstViewController] + 411
11  CarbonKit                           0x00000001048bc90e -[CarbonTabSwipeNavigation initWithItems:delegate:] + 350
12  A8Flow                              0x0000000104db69f2 $sSo24CarbonTabSwipeNavigationC5items8delegateABSayypGSg_yptcfcTO + 242
13  A8Flow                              0x0000000104db4bc2 $sSo24CarbonTabSwipeNavigationC5items8delegateABSayypGSg_yptcfC + 50
14  A8Flow                              0x0000000104db40ba $s6A8Flow6MyTaskC11viewDidLoadyyF + 378
15  A8Flow                              0x0000000104db4dcb $s6A8Flow6MyTaskC11viewDidLoadyyFTo + 43
16  UIKitCore                           0x00007fff471cdb45 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 83
17  UIKitCore                           0x00007fff471d2a9e -[UIViewController loadViewIfRequired] + 1084
18  UIKitCore                           0x00007fff47136b8c -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 160
19  UIKitCore                           0x00007fff47136e8c -[UINavigationController _startTransition:fromViewController:toViewController:] + 140
20  UIKitCore                           0x00007fff47137d56 -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
21  UIKitCore                           0x00007fff471390c1 -[UINavigationController __viewWillLayoutSubviews] + 150
22  UIKitCore                           0x00007fff47119ef7 -[UILayoutContainerView layoutSubviews] + 217
23  UIKitCore                           0x00007fff47d34cfd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
24  QuartzCore                          0x00007fff2b138d41 -[CALayer layoutSublayers] + 255
25  QuartzCore                          0x00007fff2b13ef33 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
26  QuartzCore                          0x00007fff2b14a86a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
27  QuartzCore                          0x00007fff2b0917c8 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
28  QuartzCore                          0x00007fff2b0c6ad1 _ZN2CA11Transaction6commitEv + 643
29  UIKitCore                           0x00007fff47867481 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
30  CoreFoundation                      0x00007fff23bb204c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
31  CoreFoundation                      0x00007fff23bb17b8 __CFRunLoopDoBlocks + 312
32  CoreFoundation                      0x00007fff23bac644 
__CFRunLoopRun + 1284
33  CoreFoundation                      0x00007fff23babe16 
CFRunLoopRunSpecific + 438
34  GraphicsServices                    0x00007fff38438bb0 
GSEventRunModal + 65
35  UIKitCore                           0x00007fff4784fb68 
UIApplicationMain + 1621
36  A8FlowSampleApp                     0x000000010458a6bb main + 75
37  libdyld.dylib                       0x00007fff51a1dc25 start + 1
38  ???                                 0x0000000000000001 0x0 + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type 
 NSException
 (lldb) 

[非常感谢的任何帮助..尝试了很多方法来实现它,但没有成功。

ios swift uiviewcontroller xib carbonkit
1个回答
0
投票

修复了我自己

func firstView() -> UIViewController {

    let cont = FirstViewController()
    self.tabContView?.addChild(cont)
    self.tabContView?.view.addSubview(cont.view)
    cont.didMove(toParent: tabContView)
    return cont
 }

public func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {
print("called carbonkit")       
print("carbon kit index 0 called")

return firstView()

}
© www.soinside.com 2019 - 2024. All rights reserved.