简体   繁体   中英

How can I call a function on a MasterVC, executed by the dismissal of a pushed VC?

I have a MasterVC with a UIImageView with an alpha of 0, which switches alpha to 1 on the press of a UIButton . The same button press pushes to a SecondaryVC displaying an image over the MasterVC with "Over Current Context".

If the SecondaryVC is dismissed, I want to set the alpha of the MasterVC's UIImageView back to 0, but am having difficulty finding how to call a function using only a dismiss to return to the MasterVC.

Here is my button to set the alpha and push to the SecondaryVC. Using a segue back seems out of the question since I've heard it creates memory issues.

I tried using a protocol, but it wasn't playing well with CGFloat.

@IBOutlet weak var background: UIImageView!
@IBOutlet weak var backgroundTable: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        background.image = lightBG
        backgroundTable.image = tableBG
        backgroundTable.alpha = 0
    }

@IBAction func unknownProperties(_ sender: UIButton) {
    UIView.animate(withDuration: 1.0, animations:{
        self.backgroundTable.alpha = 1
    })
    getOverlay()
}

Use of NotificationCenter could be a solution here .

Step 1 : Add an observer at viewDidLoad

NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.functionToExecute),
            name: NSNotification.Name.init("UpdateBackgroundTableAlpha"),
            object: nil
        )

Step 2 : Post a notification just before the SecondaryVC is dismissed .

NotificationCenter.default.post(name: NSNotification.Name.init("UpdateBackgroundTableAlpha"), object: nil)

Step 3 : Define functionToExecute in MasterVC class scope .

func functionToExecute(){

   print("Make BackgroundTable Great Again!")
}

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