Chris Byatt
Chris Byatt

Reputation: 3829

Set Delegate as Existing View Controller

I'm setting a delegate on a segue, but I need it to be set to the existing SfnViewController rather than what appears to be happening here which is creating a new one.

How can I access the existing SfnViewController to pass it through?

class ViewerListController {

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        editViewer = fetchedResultController.objectAtIndexPath(indexPath) as? Viewer
        performSegueWithIdentifier("editViewer", sender: indexPath)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "editViewer" {
            let editViewerViewController = segue.destinationViewController.topViewController as! EditViewerViewController
            editViewerViewController.viewer = editViewer

            //Need this as the existing SfnViewController
            editViewerViewController.delegate = SfnViewController()
        }
    }
}

Upvotes: 0

Views: 47

Answers (2)

Duncan C
Duncan C

Reputation: 131471

We can't really help you because you haven't provided enough information. You say you have an existing SfnViewController.

  • When and where was this object created?
  • Who owns it?
  • Does your ViewListController have a reference to it?

If your only have one shared SfnViewController for you entire object you should consider following the singleton design pattern and making your SfnViewController a singleton. Then you'd use a class method of the SfnViewController class to get a reference to the singleton.

Upvotes: 1

Pavel Smejkal
Pavel Smejkal

Reputation: 3599

If you mean using some specific controller which you can determine in the tableView:accessoryButtonTappedForRowWithIndexPath: method, then you should send it in the "sender" property when calling performSegueWithIdentifier like this:

class ViewerListController {

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        editViewer = fetchedResultController.objectAtIndexPath(indexPath) as? Viewer
        performSegueWithIdentifier("editViewer", sender: editViewer)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "editViewer" {
            let editViewerViewController = segue.destinationViewController.topViewController as! EditViewerViewController
            editViewerViewController.viewer = editViewer

            let editViewer = sender as! EditViewerViewController
            editViewerViewController.delegate = editViewer.delegate
        }
    }
}

Upvotes: 0

Related Questions