Search code examples
iosswiftnsbundle

present() method throws 'Could not load NIB in bundle: 'NSBundle


Using xCode 8 I created two TableViewControllers. I am trying to pass data from the selected cell in one TableViewController to the second.

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
}

When I debug this the data gets passed to the ExerciseViewController so that seems to work. The print line picks up the ExerciseViewController as the nibName, however an exception is thrown on the present method that it cannot find a nibName of ExerciseViewController.

2018-02-21 06:00:43.786 Monthly HIIT[1994:132476] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ExerciseViewController''

I am stumped on the issue. I added the files to the build path and that didn't work. Other questions suggested adding XIB files, but from digging around XIB files seem to be irrelevant with the storyboard I had created (although I can easily be wrong). Any help or suggestions are very much appreciated.

Thanks


Solution

  • You gave away the biggest hint for what's wrong when you said you are using a storyboard and not a XIB (previously known as a NIB) file.

    Try replacing:

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

    with

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

    Here is a tutorial that might help.