【问题标题】:present() method throws 'Could not load NIB in bundle: 'NSBundlepresent() 方法抛出 'Could not load NIB in bundle: 'NSBundle
【发布时间】:2018-02-22 03:31:49
【问题描述】:

我使用 xCode 8 创建了两个 TableViewController。我正在尝试将数据从一个 TableViewController 中的选定单元格传递到第二个。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){        
    let currentCell = tableView.cellForRow(at: indexPath) as! WorkOutCell
    let workout = WorkOut()
    selectedWorkout = workout.getWorkOutByName(workoutName: currentCell.workoutNameLabel.text!)
    let evc = ExerciseViewController(nibName: "ExerciseViewController", bundle: nil)
    evc.selectedWorkout = selectedWorkout //sets the selected workout value in ExerciseViewControler
    print(evc.nibName as Any) //prints Optional("ExerciseViewController")
    present(evc, animated: true, completion: nil) //throws exception
}

当我对此进行调试时,数据被传递给了 ExerciseViewController,这样似乎就可以工作了。打印行选择了 ExerciseViewController 作为 nibName,但是在 present 方法上抛出了一个异常,它找不到 ExerciseViewController 的 nibName。

2018-02-21 06:00:43.786 每月 HIIT[1994:132476] *** 终止应用 由于未捕获的异常“NSInternalInconsistencyException”,原因: '无法在包中加载NIB:'NSBundle (已加载)',名称为 'ExerciseViewController''

我对这个问题感到困惑。我将文件添加到构建路径,但没有奏效。其他问题建议添加 XIB 文件,但通过挖掘 XIB 文件似乎与我创建的故事板无关(尽管我很容易出错)。非常感谢任何帮助或建议。

谢谢

【问题讨论】:

  • 让 storyboard = UIStoryboard(name: "Storyboard Name", bundle: nil);让 viewController = storyboard.instantiateViewController(withIdentifier: "Storyboard ID") as! UINavigationController;self.present(viewController, animated:true, completion: nil)
  • 感谢@michael-dautermann 和 El Tomato,这是我缺少的部分。非常感谢您的 cmets 帮助解决这个问题。
  • @D.Charlton 如果下面迈克尔的回答解决了您的问题,您应该通过单击答案左侧的复选标记来表明这一点。

标签: ios swift nsbundle


【解决方案1】:

当您说您使用的是故事板而不是 XIB(以前称为 NIB)文件时,您给出了最大的问题提示。

尝试替换:

let evc = ExerciseViewController(nibName: "ExerciseViewController", bundle: nil)

let evc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ExerciseViewController") as ExerciseViewController

Here is a tutorial 这可能会有所帮助。

【讨论】:

    【解决方案2】:

    试试这个自定义单元格(swift 4)

    Add below code in => func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    
            var cell = tblView.dequeueReusableCell(withIdentifier: "cell") as? CustomCell
    
            if cell == nil
            {
                tblView.register(UINib(nibName: "YourNIBname/XIBname", bundle: nil), forCellReuseIdentifier: "cell")
                cell = tblView.dequeueReusableCell(withIdentifier: "cell") as? CustomCell
            }
            return cell!
    

    【讨论】:

    • 该问题与自定义单元格无关。
    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多