Search code examples
iosswiftsegueparent-child

Accessing parent view from modal segue Swift


I have a View Controller - NewsController which has a modal segue going to NewsDetailController

In NewsDetailController I am trying to run the following function:

@IBAction func greenLikeClick(sender: AnyObject) {

    if let parentVC = self.parentViewController as? NewsController {
        parentVC.newslists[loadsection][loadrow].liked = false
    }
}

But this won't work.

Is there a parent-child relationship going on between NewsController and NewsDetailController here?

If so, why isn't my approach working?

If not, how would I approach this?


Solution

  • You should use self.presentingViewController in your NewsDetailControllerto get your NewsController. From the documentation:

    When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that was presented has this property set to the view controller that presented it.