You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在iOS SDK中添加并加载Storyboard?加载报错问题解决

iOS SDK加载Storyboard报错"Could not find a storyboard named 'Test' in bundle"的解决方法

你在开发iOS SDK时,使用Storyboard设计界面,运行以下代码时出现报错:

let storyBoard: UIStoryboard = UIStoryboard(name: "Test", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController

controller.present(newViewController, animated: true, completion: nil)

错误信息:

Thread 1: "Could not find a storyboard named 'Test' in bundle

以下是具体的解决步骤:

  • 确认Storyboard文件配置

    1. 检查文件名:确保你的Storyboard文件确实是Test.storyboard,iOS系统对文件名大小写敏感,需完全匹配
    2. 检查Target归属:在Xcode中选中Test.storyboard,查看右侧Identity and Type面板的Target Membership,确保勾选了你的SDK target
    3. 检查构建阶段:进入SDK target的Build Phases,确认Test.storyboard已添加到Copy Bundle Resources列表中
  • 正确指定SDK的Bundle
    加载SDK内部的资源时,不能传nil(nil会默认使用主App的Bundle),需要获取SDK自身的Bundle:

    // 方式1:通过SDK内的类获取Bundle
    guard let sdkBundle = Bundle(for: YourSDKAnyClass.self) else {
        fatalError("Failed to get SDK bundle")
    }
    // 方式2:通过Bundle Identifier获取(替换为你的SDK的Bundle ID)
    // guard let sdkBundle = Bundle(identifier: "com.yourcompany.YourSDK") else {
    //     fatalError("Failed to get SDK bundle by identifier")
    // }
    
    let storyBoard = UIStoryboard(name: "Test", bundle: sdkBundle)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController
    controller.present(newViewController, animated: true, completion: nil)
    

    注:YourSDKAnyClass替换为你SDK中任意一个类的类名

  • 验证ViewController的Storyboard ID
    打开Test.storyboard,选中目标ViewController,在右侧Identity Inspector面板中:

    1. 确认Storyboard ID设置为TestViewController
    2. 如果不需要将其设为初始控制器,无需勾选Is Initial View Controller,但必须设置Storyboard ID才能通过标识符实例化
  • 清理缓存并重新构建

    1. 按Command + Shift + K清理项目
    2. 按Command + Option + Shift + K清理构建缓存
    3. 重新运行项目,避免缓存导致的资源加载异常

内容的提问来源于stack exchange,提问作者Tcoat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:33:29