加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在’didSelectRowAtIndexPath’中访问segue – Swift / IOS

发布时间:2020-12-14 05:28:18 所属栏目:百科 来源:网络整理
导读:我知道如何使用prepareForSegue函数中的segue传递数据,但是我有一个TableViewCell,它有两个可能的段到两个不同的ViewControllers(我们现在就说A,B).建议 here最好将segue连接到View控制器而不是tableCell本身,这实际上是完美的.但是我想在单击单元格时将信息
我知道如何使用prepareForSegue函数中的segue传递数据,但是我有一个TableViewCell,它有两个可能的段到两个不同的ViewControllers(我们现在就说A,B).建议 here最好将segue连接到View控制器而不是tableCell本身,这实际上是完美的.但是我想在单击单元格时将信息传递给第二个View控制器,那么如何访问我连接到Source ViewController的segue.

码:

在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传递信息

您应该使用didSelectRowAtIndexPath方法来确定是否选择了单元格.
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
    }
}

解释…

>我使用索引路径作为发送方,因此我可以轻松地传递索引路径.您还可以使用其他UITableView方法检查当前选定的单元格,但我总是以这种方式成功完成>你不能将performSegueWithIdentifier放在准备segue方法中,因为performSegueWithIdentifier会导致prepareForSegue;你只是四处走动,没有目标. (当你想执行segue时,总是会执行prepareForSegue)>选择行时,prepareForSegue不会自行运行.这是您需要didSelectRowAtIndexPath的地方.你需要在前面描述的方法之外的performSegueWithIdentifier,它应该在didSelectRowAtIndexPath中

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读