-1

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

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
D. Charlton
  • 13
  • 1
  • 2
  • 1
    Try [searching on the error](https://stackoverflow.com/search?q=Could+not+load+NIB+in+bundle+with+name+%5Bswift%5D). – rmaddy Feb 22 '18 at 03:35
  • 1
    let storyboard = UIStoryboard(name: "Storyboard Name", bundle: nil);let viewController = storyboard.instantiateViewController(withIdentifier: "Storyboard ID") as! UINavigationController;self.present(viewController, animated:true, completion: nil) – El Tomato Feb 22 '18 at 03:36
  • Thanks @michael-dautermann and El Tomato that was the part I was missing. Really appreciate your comments in helping figure this out. – D. Charlton Feb 22 '18 at 03:57
  • @D.Charlton If Michael's answer below solved your issue you should indicate this by clicking the checkmark to the left of the answer. – rmaddy Feb 22 '18 at 05:37

2 Answers2

4

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

Try this for custom cell (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!
Nick
  • 875
  • 6
  • 20