将数据从UIViewController发送到UITableViewController并在Swift中返回

时间:2014-07-18 11:47:53

标签: uitableview uiviewcontroller swift ios8

我有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.但是,UIViewControllerprintln("Received index: \(currentCityIndex)")显示为0!

为什么呢?有什么问题?

1 个答案:

答案 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)
}