Omar Alhadidy
Omar Alhadidy

Reputation: 109

referencing parent view controller in swift

This code is inside child view controller

@IBAction func send(_ sender: UIButton) {


    let parentVC = (self.navigationController?.parent)! as! sendController

    parentVC.sendText.text = ""

    self.removeFromParentViewController()
    self.view.removeFromSuperview()

}

When the button is pressed, the app crashes with the error "EXC_BAD_INSTRUCTION code i386"

Am I referencing the parent view controller the right way?

Upvotes: 0

Views: 5724

Answers (2)

Mavi
Mavi

Reputation: 77

Also, if the View Controller was presented (by this I mean not pushed through a Navigation Controller), you can access the parent with this property in Swift:

self.presentingViewController

Here is the documentation about the property: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller

Upvotes: 3

AkhilaHaridas
AkhilaHaridas

Reputation: 11

you can access parent view controller as 'self.parent' in Swift 3.

Upvotes: 0

Related Questions