Scott
Scott

Reputation: 1478

Programmatically unwind segue

I am displaying a UIView with a UITableView control on it. I display this view with a Show segue, by calling performSegueWithIdentifier. In the module from which the segue is called, I have added a function that I want to use on unwind, to retrieve some data from the view upon return from the segue. Right now, I have a Done button that is hooked up to the function on the caller, so that I can retrieve the data from the segue when it unwinds. Instead of doing this all when the Done button is tapped, I would like to do it when an item is selected in the UITableView.

In tableView(,willSelectRowAtIndexPath), I try calling popViewControllerAnimated. The second UIView does close and bring me back to the calling UIView, but the unwind function is not called.

I know how to set up the unwind from the done button, using Exit, but I cannot figure out how to do it when tapping on a row in the UITableView.

Here's the code that I'm using to execute the segue:

    @IBAction func fromButtonTap(sender: AnyObject) {
    currentButton = sender.tag

    self.performSegueWithIdentifier("UnitsSegue", sender: self)
}

Here's the code that executes when the segue is unwound:

@IBAction func returnFromUnitSelection(segue: UIStoryboardSegue) {
    var buttonToSet: UIButton!
    var unitsToSet: UnitData!

    fromLabel.text = "0"
    toLabel.text = "0"

    if let unitsSelectionViewController = segue.sourceViewController as? UnitSelectionController {
        if currentButton == 0 {
            fromUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = fromButton
        }
        else {
            toUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = toButton
        }

        setButtonText(buttonToSet, firstLine: unitsSelectionViewController.selectedUnit.unitCode, secondLine: unitsSelectionViewController.selectedUnit.unitName, firstLineFontSize: 15, secondLineFontSize: 11)
    }
}

Here's the code that I use to close the second UIView:

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    selectedUnit = temperatures[indexPath.row]
    self.navigationController?.popViewControllerAnimated(true)

    return indexPath
}

The unwind code does execute when I tap Done. But nothing happens when I select an item in my table.

Upvotes: 4

Views: 3022

Answers (1)

mustafa
mustafa

Reputation: 15464

You can create unwind segue for entire view controller. To do this drag segue from view controller objet to exit object. enter image description here

After segue created it is listed in document outline.

enter image description here

Select it and give it an identifier in attributes inspector. Then inside your code when when tableview selects a cell perform this segue like any other normal segue.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let title = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text

    performSegueWithIdentifier("backToViewController1", sender: title)
}

Then override prepareForSegue method to pass data to first view controller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier where identifier == "backToViewController1",
       let destination = segue.destinationViewController as? ViewController,
           selectedTitle = sender as? String {
        destination.selectedCountryTitle = selectedTitle
    }
}

Upvotes: 4

Related Questions