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

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

iphone - Segue is being executed twice

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

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...