Segue正在执行两次

时间:2016-09-05 17:13:21

标签: iphone swift xcode

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   self.selectedClubState = stateNamesForDisplay[indexPath.row]
   self.performSegueWithIdentifier ("Cities", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var clubsToPassToCitiesViewController = [clubObject]()
    if segue.identifier == "Cities" {
        for club in clubsForTable{
            if club.clubState == self.selectedClubState{
                clubsToPassToCitiesViewController.append(club)
            }
        }
       let citiesView = segue.destinationViewController as? citiesViewController
       citiesView?.clubsForChosenCity = clubsToPassToCitiesViewController
   }
}

Segue正在执行两次导致下一个VC。我怎样才能防止这种情况发生?

2 个答案:

答案 0 :(得分:10)

是两次执行segue的人 - 一次自动在故事板中(因为你的segue是从单元格原型中发出的一个Action Segue),一次是在代码中你说{{1} }。如果您不希望segue执行两次,请删除其中一个。

就我个人而言,我的建议是您完全删除self.performSegueWithIdentifier并将didSelectRow作业移至self.selectedClubState

答案 1 :(得分:9)

删除故事板中的当前segue。然后按住CTRL键将从viewController (不是单元格)拖到下一个视图控制器,并将其命名为“Cities”。现在,当您选择一个单元格时,didSelectRowAtIndexPath()将首先触发,然后调用performSegueWithIdentifier()

enter image description here

但是,如果您要在didSelectRowAtIndexPath()中查找的所有内容都获得执行segue的行,则可以保留原始设置,即从故事板中获取单元格,删除didSelectRowAtIndexPath()并在prepareForSegue()做:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let indexPath = self.tableView.indexPathForSelectedRow {
        self.selectedClubState = stateNamesForDisplay[indexPath.row]
    }
    var clubsToPassToCitiesViewController = [clubObject]()
    if segue.identifier == "Cities" {
        for club in clubsForTable{
            if club.clubState == self.selectedClubState{
                clubsToPassToCitiesViewController.append(club)
            }
        }
       let citiesView = segue.destinationViewController as? citiesViewController
       citiesView?.clubsForChosenCity = clubsToPassToCitiesViewController
   }
}