简体   繁体   中英

ios Swift ViewController Loaded from Selecting TableView Cell Loses Reference

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()
}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM