我有UIViewController
嵌入式NavigationController
(CurrentCityViewController.swift),它在UITableViewController(CitiesListTableViewController.swift)中发送数据。这是UIViewController
中代码的一部分:
......
var currentCityIndex: Int = 0
var resultWeatherArray: [(city:String, country:String, image:UIImage, weather:String, temp:String, humidity:String, wind:String, windspeed:String, pressure:String)] = []
var isFirstStart: Bool = true
override func viewDidAppear(animated: Bool) {
if isFirstStart == true {
println("First start")
isFirstStart = false
......
}
else {
println("Not first start")
println("Received index: \(currentCityIndex)")
localCityName.text = resultWeatherArray[currentCityIndex].city
......
}
}
......
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if segue.identifier? == "showFavCities" {
let destViewController:CitiesListTableViewController! = segue.destinationViewController as CitiesListTableViewController
destViewController.resultWeatherArray = self.resultWeatherArray
}
}
在UITableViewController
我选择了一行。接下来,我想将行号传回UIViewController并使用该号码。但! println("Received index: \(currentCityIndex)")
中的字符串UIViewController
始终显示为0!这是代码UITableViewController
:
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
currentCityController.currentCityIndex = self.tableView.indexPathForSelectedRow().row
println("Index for send: \(currentCityController.currentCityIndex)")
self.navigationController.popViewControllerAnimated(true)
}
例如,我在UITableViewController
中选择表格中的第二行。 println("Index for send: \(currentCityController.currentCityIndex)")
显示1.但是,UIViewController
行println("Received index: \(currentCityIndex)")
显示为0!
为什么呢?有什么问题?
答案 0 :(得分:0)
解决!在UITableViewController
:
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var currentCityController = self.navigationController.viewControllers[0] as CurrentCityViewController
currentCityController.currentCityIndex = self.tableView.indexPathForSelectedRow().row
self.navigationController.popViewControllerAnimated(true)
}