对于学校项目,我正在制作一个有关大屠杀的应用程序。我需要在detailViewController中显示有关集中营的信息,但它无法正常工作。我能够访问变量的“名称”值(如果我的术语不正确,请谅解),但没有其他信息。其他帖子不是使用Swift编写的,或者与我的特定问题无关,因此我找不到解决方案。
这是创建变量类型的类中的代码:
这是创建变量类型的类中的代码:
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
}
}
这里是设置细节项到数组中的代码:
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
}
}
}
针对评论中的建议,这里是我的完整代码,用于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.
}
}
configureView
属于哪个类?谁调用它?发布DetailViewController
的代码。 - ryantxr