【问题标题】:XCode 6: I can't select a view by its storyboard IDXCode 6:我无法通过故事板 ID 选择视图
【发布时间】:2015-07-26 02:46:24
【问题描述】:

这是代码:

let theBoard = self.storyboard!
let vc = theBoard.instantiateViewControllerWithIdentifier("myViewId")  as! UIViewController // this line causes the error

第二行导致这个错误:

unexpectedly found nil while unwrapping an Optional value

* 非常重要 * :该错误仅在构建配置设置为“debug”时触发。设置为“release”时效果很好。

为什么?

【问题讨论】:

  • 你确定当你发布它时会显示一个目标故事板?
  • 绝对,在发布时视图显示
  • 清理项目并重建
  • 做了几次,derivedData也被删除了
  • 你确认它不是 theBoard 是 nil 吗?

标签: ios objective-c xcode swift storyboard


【解决方案1】:

您将 UIViewController 投射到 let vc = theBoard.instantiateViewControllerWithIdentifier("myViewId") as! UIViewController 中,这是错误的。 你应该试试:

var storyboard = UIStoryboard(name: "Main", bundle: nil)

var vc = storyboard.instantiateViewControllerWithIdentifier("myViewId") as! ViewController

【讨论】:

    【解决方案2】:

    这都是新语法的问题,功能没有改变:

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
    

    【讨论】:

    • 我试过这个解决方案,但为什么它在发布而不是在调试中有效?
    • 某处可能存在一些配置错误。这就是我要找的。​​span>
    【解决方案3】:

    这样使用

    var moveToNextVC:ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myViewId") as! ViewController
    
     self.presentViewController(moveToNextVC, animated: true, completion: nil)
    

    记住在文件检查器中为您的视图控制器设置 StoryboardID 识别“myViewId”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-09-11
      相关资源
      最近更新 更多