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.
对于学校,我正在制作关于大*的应用程序。我需要在detailViewController上显示有关阵营的信息,但它无法正常工作。我能够访问变量的“名称”值(对不起,如果我的术语错误),但没有别的。其他帖子不在Swift中或与我的具体问题无关,所以我找不到解决方案。
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:
回应评论的建议,这是我的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.
}
}
1 个解决方案
#1
1
Change
if let detail = self.detailItemDeath
如果让detail = self.detailItemDeath
to:
if let detail = self.detailItemDeath as? DeathCamp
如果让detail = self.detailItemDeath为? DeathCamp
The code above attempts a cast of detailItemDeath
to an object of type DeathCamp
.
上面的代码尝试将detailItemDeath转换为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.
您已尝试使用if let语法展开detailItemDeath,可选链接是多余的,因此是错误的。
And, I can't believe I missed this, rename detailItemDeath
in detailItemDeath.name
to detail
!
而且,我无法相信我错过了这一点,将detailItemDeath中的detailItemDeath重命名为detail!
#1
1
Change
if let detail = self.detailItemDeath
如果让detail = self.detailItemDeath
to:
if let detail = self.detailItemDeath as? DeathCamp
如果让detail = self.detailItemDeath为? DeathCamp
The code above attempts a cast of detailItemDeath
to an object of type DeathCamp
.
上面的代码尝试将detailItemDeath转换为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.
您已尝试使用if let语法展开detailItemDeath,可选链接是多余的,因此是错误的。
And, I can't believe I missed this, rename detailItemDeath
in detailItemDeath.name
to detail
!
而且,我无法相信我错过了这一点,将detailItemDeath中的detailItemDeath重命名为detail!