Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 is being executed twice leading to the next VC. How can I prevent this from happening?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
686 views
Welcome To Ask or Share your Answers For Others

1 Answer

Delete the current segue in storyboard. Then CTRL-drag from the viewController (not the cell) to the next view controller and name it "Cities". Now, when you select a cell, the didSelectRowAtIndexPath() will fire first and will call performSegueWithIdentifier()

enter image description here

However, if all you're looking to do in the didSelectRowAtIndexPath() is get the row that performed the segue, you can maintain your original setup of having the cell segue from the storyboard, remove didSelectRowAtIndexPath() and in prepareForSegue() do:

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
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...