0

Full code for this branch here

View controller "MovieDetailsVC" is presented to the navigation controller when a cell is selected.

The presenting view controller, "ViewController", stores the row of the tableView to display in NSUserDefaults as an Int.

"MovieDetailsVC" reads the row ok. It then pulls the whole array of custom class info from CoreData and stores the array row in a property.

The data is displayed ok at first. The IBOutlet connections are setup ok. I've disconnected and reconnected twice all outlets on MovieDetailsVC, so that should be ok.

"viewDidLoad" is called a successive time. Not sure from where. When it is called, the coredata entity and row number are pulled ok.

The issue is at line "lblTitle.text = self.movieRecord.title". I'm assuming any of the IBOutlets would cause the same issue.

The error thrown is what you would see if the outlets were not connected:

fatal error: unexpectedly fond nil while unwrapping Optional value.

code for the MovieDetailsVC is below. Any ideas why this outlet link would break after working ok would be greatly appreciated.

import UIKit
import CoreData

class MovieDetailsVC: UIViewController {

@IBOutlet var lblTitle: UILabel!
@IBOutlet var lblDescr: UILabel!
@IBOutlet var lblLink: UILabel!
@IBOutlet var imgMovie: UIImageView!


var movieRecord:FavMovie!

var favMovies = [FavMovie]()

override func viewDidLoad() {
    super.viewDidLoad()

    fetchAndSetResult()

}

func fetchAndSetResult() {


    let app = UIApplication.sharedApplication().delegate as! AppDelegate
    let context = app.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "FavMovie")

    do {
        let results = try context.executeFetchRequest(fetchRequest)
        self.favMovies = results as! [FavMovie]
    } catch let err as NSError {
        print(err.description)
    }

    if let row = NSUserDefaults.standardUserDefaults().objectForKey("movieRow") as? Int {

        self.movieRecord = self.favMovies[row]

        configureCellDescr()

    }


}

func configureCellDescr() {
    lblTitle.text = self.movieRecord.title
    lblDescr.text = self.movieRecord.descrWhyGood
    lblLink.text = self.movieRecord.linkImdb
    imgMovie.image = self.movieRecord.getImg()
}

}
Emzor
  • 1,380
  • 17
  • 28
Michael James
  • 492
  • 1
  • 6
  • 19
  • do you mean the codes in configureCellDescr() method cause the error. If you are not sure which line of code cause error, you should add a exception breakpoint to track it. – Surely Feb 20 '16 at 18:22
  • Edited to show the line where the code is failing. – Michael James Feb 20 '16 at 18:27
  • Apologies, edited to show line throwing error – Michael James Feb 20 '16 at 18:28
  • Did you check if the `movieRecord` object is not `nil`? This might also cause the code to fail at that line, if you are sure the outlets are connected right. – Catalina T. Feb 20 '16 at 20:20
  • Yes. When setting a break point to the line where the error is thrown, I've inspected self.movieRecord.title, and it is populated with a value, not nil. – Michael James Feb 20 '16 at 20:22
  • what is the value of lblTitle.text at time of assigning self.movieRecord.title to it – hariszaman Feb 20 '16 at 20:53
  • Trying to inspect lblTitle.text returns nil and gives the error discussed in the post. That is, it does that the second time that the app overrides viewdidload. The first time, it works OK. – Michael James Feb 20 '16 at 20:57

1 Answers1

1

I just have a look at your source code in github and find the problem. There are two issues and I will explain it following.

it does that the second time that the app overrides viewdidload

The reason that your code would call the viewDidLoad method twice is because you have a segue in your storyboard that connect the tableViewCell to movieDetailVC. This will present the movieDetailVC once you click the cell.

And in your code for didSelectCell method, you create another movieDetailVC object and present it.

So actually movieDetailVC would be presented twice when you click the cell. This cause the issue.

Any ideas why this outlet link would break after working ok would be greatly appreciated

The reason why the IBOutlet is nil is because of the way you present movieDetailVC in your code. You create the movieDetailVC object using: let movieDetailsVC = MovieDetailsVC(). Doing it this way, the IBOutlets will not be connected correctly, because ios dont know about the storyboard information.

The correct way to create a MovieDetailVC object is to instantiate from storyboard. See this post for difference.

Solution

There is a very simple solution for your code design:

just remove let movieDetailsVC = MovieDetailsVC() and self.navigationController?.presentViewController(movieDetailsVC, animated: true, completion: nil) from your ViewController class. Since you save the selection data in NSUserDefault, the cell segue will present movieDetailVC and your movieDetailVC can also get the selection data from userDefault.

Community
  • 1
  • 1
Surely
  • 1,649
  • 16
  • 23