首页 > 解决方案 > 无法从私有 cocoapods 框架访问故事板和视图控制器文件

问题描述

我创建了自己的私有框架并将其用作 iOS 项目中的 cocoapods,并且我能够从框架访问所有 swift 文件,但是当我尝试显示框架故事板文件中存在的屏幕时,我遇到了崩溃在 iOS 项目中。

详情如下:

错误:

2020-06-06 18:02:30.469084+0530 Test[33726:4822657] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x6000024c7300>) doesn't contain a view controller with identifier 'AdditionVC''
*** First throw call stack: 
(   
0   CoreFoundation                      0x0000000106a4c8db __exceptionPreprocess + 331  
1   libobjc.A.dylib    0x0000000104fd7ac5 objc_exception_throw + 48     
2   UIKitCore            0x00000001094206a9 -[UIStoryboard instantiateInitialViewController] + 0    
3   Framework                           0x00000001049dcef6 $s22Framework7ManagerC10additionVCSo16UIViewControllerCyFZ + 262     
4   Test                                0x00000001046d8932 $s17Test14ViewControllerC11viewDidLoadyyF + 178  
5   Test 0x00000001046d8a9b $s17Test14ViewControllerC11viewDidLoadyyFTo + 43    
6 UIKitCore                           0x0000000108c820f7
    -[UIViewController loadViewIfRequired] + 1183   
7   UIKitCore                           0x0000000108be5cf0 -[UINavigationController
    _updateScrollViewFromViewController:toViewController:] + 68     
8   UIKitCore                           0x0000000108be5fe3
    -[UINavigationController _startTransition:fromViewController:toViewController:] + 146   
9   UIKitCore                           0x0000000108be70a1
    -[UINavigationController _startDeferredTransitionIfNeeded:] + 896   
10  UIKitCore                           0x0000000108be8393
    -[UINavigationController __viewWillLayoutSubviews] + 150    
11  UIKitCore                           0x0000000108bc9041
    -[UILayoutContainerView layoutSubviews] + 217   
12  UIKitCore                           0x0000000109763e69 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
    + 1417  
13  QuartzCore                          0x000000010ace6d22 -[CALayer layoutSublayers] + 173     
14  QuartzCore                          0x000000010aceb9fc _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396  
15  QuartzCore                          0x000000010acf7d58
    _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72  
16  QuartzCore                          0x000000010ac6724a
    _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328     
17  QuartzCore                          0x000000010ac9e606
    _ZN2CA11Transaction6commitEv + 610  
18  UIKitCore                           0x000000010929e2c3 __34-[UIApplication
    _firstCommitBlock]_block_invoke_2 + 128     
19  CoreFoundation                      0x00000001069b3cbc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12     
20  CoreFoundation                      0x00000001069b3480
    __CFRunLoopDoBlocks + 336   
21  CoreFoundation                      0x00000001069add04 __CFRunLoopRun + 1252    
22  CoreFoundation           0x00000001069ad4d2 CFRunLoopRunSpecific + 626  
23  GraphicsServices    0x000000010eab12fe GSEventRunModal + 65     
24  UIKitCore                 0x0000000109284fc2 UIApplicationMain + 140    
25  Test                   0x00000001046d990b main + 75     
26  libdyld.dylib                       0x0000000107de8541 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException

框架结构:

在此处输入图像描述

Podspec 文件

Pod::Spec.new do |spec|

  spec.name         = "Framework"
  spec.version      = "0.0.4"
  spec.summary      = "Framework to give support to main project."
  spec.description  = "Framework is developed to give additional support to any normal user to create their private profile by adding it in main project."
  spec.homepage     = "https://github.com/my/Framework"
  spec.license      = { :type => "MIT", :file => "LICENSE" }
  spec.author       = { "Tushar Amkar" => "tusharamkar@gmail.com" }
  spec.platform     = :ios, "12.0"
  spec.source       = { :git => "https://github.com/my/Framework.git", :tag => "#{spec.version}" }
  spec.source_files  = "Framework/Framework/**/*.{swift,otf,storyboard}"
  spec.resource_bundles = {
    'Framework' => ['Framework/Framework/**/*.{storyboard}']
  }
  spec.exclude_files = "Classes/Exclude"
  spec.swift_version = "5.0"

end

我试图显示从不同参考网站获得但无法正常工作的视图控制器文件的代码:

代码 1

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AdditionVC") as! AdditionVC
self.navigationController?.pushViewController(controller, animated: true)

代码 2

let podBundle = Bundle(for: AdditionVC.self)
if let bundleURL = podBundle.url(forResource: "Framework", withExtension: "bundle") {
   if let bundle = Bundle(url: bundleURL) {
      let storyBoard = UIStoryboard.init(name: "Main", bundle: bundle)
      if let vc = storyBoard.instantiateViewController(withIdentifier: "AdditionVC") as? AdditionVC {
          self.navigationController?.pushViewController(vc, animated: true)
      }
   }
}

我已经检查了我们目前正在使用它的其他 pod,所以它工作正常下面是代码(虽然它是客观的,但我认为在这种情况下语言并不重要):

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Applozic" bundle:[NSBundle bundleForClass:[self class]]];
ALGroupDetailViewController * groupDetailViewController = (ALGroupDetailViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ALGroupDetailViewController"];
groupDetailViewController.channelKeyID = self.channelKey;
groupDetailViewController.alChatViewController = self;
[self.navigationController pushViewController:groupDetailViewController animated:YES];

欢迎任何帮助。提前致谢。

标签: iosswiftxcodeframeworkscocoapods

解决方案


在查看了所有可能的解决方案之后,我尝试更改模拟器并且它工作正常,并且我已经编写了代码,我们通常遵循如下:

let storyboard = UIStoryboard(name: "WodPodMain", bundle: Bundle(for: AdditionVC.self))
if let vc = storyboard.instantiateViewController(withIdentifier: "AdditionVC") as? AdditionVC{
    self.navigationController?.pushViewController(vc,animated: true)
}

所以这个问题很奇怪,模拟器有问题!!!


推荐阅读