Xcode cannot find referenced Storyboard using Cocoapods

末鹿安然 提交于 2019-12-04 18:44:02

问题


I'm trying to create a Pod using CocoaPods and I want to bundle a "Demo" Storyboard that I can reference to from the Main Storyboard from my example application. The problem is that Xcode gives me the following compiler error when I do so:

Did not find storyboard named "Demo" referenced from Main.storyboard

See:

In my Podspec, I included:

s.resource_bundles = {
    'StoryboardAssets' => ['Pod/Assets/*.{storyboard,png}']
}

You can find the demo repository that I created using the "Using Pod Lib Create" guide, you can find it here:

https://github.com/Kukiwon/StoryboardDemo

I'm running CocoaPods version 0.39.0.

So what would be the proper way to reference a Storyboard from your Pod? Am I missing something?


回答1:


To reference storyboard using cocoapods, you should set in the Bundle section, the bundle identifier of the pod, like the picture below:

And in your podspec file, you should add the reference of your storyboard (as resource and not bundle)

s.resource = 'MyPod/MyStoryboardName.storyboard'

After the pod update, everything will work as expected

Hope that helps




回答2:


Files included in a Dynamic Framework are embedded in a different NSBundle in the application. To make it easier to obtain the storyboard, you can create a helper class inside your library:

public class StoryboardHelper: NSObject {
    public static let helper = StoryboardHelper()

    public lazy var storyboard: UIStoryboard! = UIStoryboard(name: "Main", bundle: NSBundle(forClass: StoryboardHelper.self))

    public func rootController() -> UIViewController! {
        return storyboard.instantiateInitialViewController()
    }
}

NSBundle(forClass: StoryboardHelper.self) will obtain the bundle of StoryboardHelper.

This way, your user only needs to do the following to show your root view controller of the embedded storyboard:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    window?.rootViewController = StoryboardHelper.helper.rootController()
    window?.makeKeyAndVisible()

    return true
}


来源:https://stackoverflow.com/questions/35501599/xcode-cannot-find-referenced-storyboard-using-cocoapods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!