如何在'didSelectRowAtIndexPath'中访问segue - Swift / IOS [英] how to access segue in 'didSelectRowAtIndexPath' - Swift/IOS

查看:83
本文介绍了如何在'didSelectRowAtIndexPath'中访问segue - Swift / IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用来自 prepareForSegue 函数的segue传递数据,但我有一个TableViewCell,它有两个可能的段到两个不同的ViewControllers(让我们说A,B截至目前)。有人建议这里最好将segues连接到View控制器而不是tableCell本身,这实际上是完美的。但是我想在单击单元格时将信息传递给第二个View控制器,那么如何访问我连接到Source ViewController的 segue

I know how to pass data using segues from prepareForSegue function, but the thing i have a TableViewCell from which there are two possible segues to two different ViewControllers (let's just say A, B as of now). It was suggested here that it is best to connect the segues to View controller rather than the tableCell itself, which infact worked perfectly. But i want to pass information to the second View controller when the cell is clicked, So how to access segue that i connected to the Source ViewController.

代码:

在prepareForSegue内部

Inside prepareForSegue

if segue.identifier == "showQuestionnaire"{
      if let indexPath = self.tableView.indexPathForSelectedRow() {
        let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController
        let patientQuestionnaire = patientQuestionnaires[indexPath.row] as! PatientQuestionnaire
        controller.selectedQuestionnaire = patientQuestionnaire
        self.performSegueWithIdentifier("showQuestionnaire", sender: self)
      }
    }

再次提出:这个问题与通过 prepareForSegue

Again: This question is not regarding passing information through prepareForSegue

推荐答案

您应该使用 didSelectRowAtIndexPath 方法来确定是否选择了单元格。

You should be using the didSelectRowAtIndexPath method to determine whether or not a cell was selected.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("showQuestionnaire", sender: indexPath);
}

然后在你的 prepareForSegue 方法

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "showQuestionnaire") {
        let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController
        let row = (sender as! NSIndexPath).row; //we know that sender is an NSIndexPath here.
        let patientQuestionnaire = patientQuestionnaires[row] as! PatientQuestionnaire 
        controller.selectedQuestionnaire = patientQuestionnaire
    }
}

To解释...


  1. 我使用索引路径作为发送方,因此我可以轻松传递索引路径。您还可以使用其他UITableView方法检查当前选定的单元格,但我总是这样成功完成

  2. 您不能放入 performSegueWithIdentifier 在准备segue方法中,因为 performSegueWithIdentifier 导致 prepareForSegue ;你只是四处走动,没有目标。 (如果要执行segue,则始终执行prepareForSegue)

  3. prepareForSegue 在选择行时不会自行运行。这是您需要 didSelectRowAtIndexPath 的地方。你需要在前面描述的方法之外的 performSegueWithIdentifier ,它应该在 didSelectRowAtIndexPath

  1. I used the index path as the sender so I can easily pass the index path. You could also check for the currently selected cell using other UITableView methods, but I've always done it this way with success
  2. You can't put performSegueWithIdentifier within the prepare for segue method, because performSegueWithIdentifier leads to prepareForSegue; You are just looping around and around with no aim. (When you want to perform a segue, prepareForSegue is always executed)
  3. prepareForSegue doesn't run by itself when a row is selected. This is where you need didSelectRowAtIndexPath. You need a performSegueWithIdentifier outside of the method as described previously, which should be in didSelectRowAtIndexPath

这篇关于如何在'didSelectRowAtIndexPath'中访问segue - Swift / IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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