基于 Swift 中的列表选择在 ViewControllers 之间传递值 [英] Passing values between ViewControllers based on list selection in Swift

查看:27
本文介绍了基于 Swift 中的列表选择在 ViewControllers 之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 listView 选择的选定索引号从一个 ViewController 传递到另一个,但我遇到了 tableView didSelectRowAtIndexPath 委托比 prepareForSegue 函数运行稍晚的问题.

I'm trying to pass the selected index number of a listView selection from one ViewController to another but am running into an issue with the tableView didSelectRowAtIndexPath delegate runs slightly later than the prepareForSegue function.

基本上,在didSelectRowAtIndexPath 中,我设置了一个变量,然后在prepareForSegue 中选取该变量.问题是 prepareForSegue 似乎在选择单元格的第二个时间运行,并且在调用 didSelectRowAtIndexPath 函数之前运行,所以我的变量没有被传递.

Basically, in didSelectRowAtIndexPath, I seta variable, which is then picked up in the prepareForSegue. The issue is that prepareForSegue seems to run the second that the cell is selected and before the didSelectRowAtIndexPath function is called so my variable is not passed.

我的主要代码是:

tableView didSelectRowAtIndexPath 委托,它设置了 'selectedResult'...

tableView didSelectRowAtIndexPath delegate, which sets 'selectedResult'...

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    selectedResult = indexPath.item
    txtNameSearch.resignFirstResponder()    //get rid of keyboard when table touched.
    println("saved result")


    //tableView.deselectRowAtIndexPath(indexPath, animated: false)
    //var tappedItem: ToDoItem = self.toDoItems.objectAtIndex(indexPath.row) as ToDoItem
    //tappedItem.completed = !tappedItem.completed
    //tableView.reloadData()

}

prepareForSegue 函数将变量作为 'toPass' 发送到另一个 ViewController('detailViewController'):

prepareForSegue function which sends the variable as 'toPass' to the other ViewController ('detailViewController'):

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
    if (segue.identifier == "detailSeque") {
        println("preparing")
        var svc = segue!.destinationViewController as detailViewController
        svc.toPass = selectedResult

        //println(tableView.indexPathsForSelectedRows().
        //svc.toPass = tableView.indexPathForSelectedRow()

    }
}

提前致谢

推荐答案

如果你的 ViewController 中有一个 tableView 的插座,你可以在 indexPathForSelectedRow 中调用 indexPathForSelectedRow代码>prepareForSegue.

If you have an outlet to your tableView in your ViewController, you can just call indexPathForSelectedRow in prepareForSegue.

如果你的 ViewControllerUITableViewController 的子类,那么你可以这样做:

If your ViewController is a subclass of UITableViewController, then you can do:

    let row = self.tableView.indexPathForSelectedRow().row
    println("row \(row) was selected")

如果您的 ViewController 不是 UITableViewController 的子类,请在您的 UITableView 中设置一个 IBOutlet视图控制器:

If your ViewController is not a subclass of UITableViewController, set up an IBOutlet to the UITableView in your view controller:

@IBOutlet var tableView: UITableView!

并将其连接到 Interface Builder 中,然后从 prepareForSegue 调用它,如上所示.

and wire that up in Interface Builder and call it from prepareForSegue as show above.

这篇关于基于 Swift 中的列表选择在 ViewControllers 之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆