简体   繁体   中英

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?

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

您可以在Swift 3中以“ self.parent”身份访问父视图控制器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM