Reputation: 1196
For school, I am making an app about the Holocaust. I need to display info about the camps on a detailViewController, but it isn't working. I am able to access the variable's "name" value (sorry if my terminology is wrong), but nothing else. Other posts aren't in Swift or don't relate to my specific issue, so I can't find a solution.
Here is the code from the class that creates the variable type:
class DeathCamp: NSObject {
var name:String = ""
var locationCountry:String = ""
var jewishDeathCount:Int = 0
init(name:String, locationCountry:String, jewishDeathCount:Int){
self.name = name
self.locationCountry = locationCountry
self.jewishDeathCount = jewishDeathCount
}
}
Here is the code where the detail items are set to the arrays:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let deathCampNewObject = deathCampArray[indexPath.row]
let countryDeathNewObject = countryArray[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItemDeath = deathCampNewObject
controller.detailItemCountry = countryDeathNewObject
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
In response to a suggestion from a comment, here is my entire code for the DetailViewController:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet var percentageJewsKilled: UILabel!
@IBOutlet var jewsKilled: UILabel!
let masterViewController = MasterViewController()
var detailItemDeath: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
var detailItemCountry: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let detail = self.detailItemDeath {
if let label = self.detailDescriptionLabel {
label.text = detailItemDeath?.name
}
if let label = self.jewsKilled {
label.text = "\(detailItemDeath?.jewishDeathCount)"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 5
Views: 8731
Reputation: 18171
Change
if let detail = self.detailItemDeath
to:
if let detail = self.detailItemDeath as? DeathCamp
The code above attempts a cast of detailItemDeath
to an object of type DeathCamp
.
Also change
detailItemDeath?.name
to:
detailItemDeath.name
You've already attempted an unwrap of detailItemDeath
with the if let
syntax, optional chaining is redundant and therefore erroneous.
And, I can't believe I missed this, rename detailItemDeath
in detailItemDeath.name
to detail
!
Upvotes: 3