How do I insert code in ViewDidAppear and ViewDidDisappear From other class

前端 未结 2 1723
广开言路
广开言路 2021-01-23 08:22

I want to change button image whenever UISideMenuNavigationController Appear Or Disappear.

This is the class that has a button.

class MenuViewController         


        
相关标签:
2条回答
  • 2021-01-23 08:46

    You simply need to subclass UISideMenuNavigationController and override the viewDidAppear & viewDidDisappear methods to invoke a delegate.

    protocol MyUISideMenuDelegate {
        func menuDidAppear(_ menu:MyUISideMenuNavigationController) -> Void
        func menuDidDisappear(_ menu:MyUISideMenuNavigationController) -> Void
    }
    
    open class MyUISideMenuNavigationController: UISideMenuNavigationController {
    
        var menuDelegate: MyUISideMenuDelegate? 
    
        override open func viewDidAppear(_ animated: Bool) {
             super.viewDidAppear(animated)
             self.menuDelegate?.menuDidAppear(self)
        }
    
        override open func viewDidDisappear(_ animated: Bool) {
            super.viewDidDisappear(animated)
            self.menuDelegate?.menuDidDisappear(self)
        }
    }
    

    Then have you view controller with the button implement the protocol and set itself as the delegate.

    You could also have your menu subclass send NSNotification and have any other objects that are interested subscribe to those. This way you completely decouple the menu and the other classes.

    0 讨论(0)
  • 2021-01-23 08:49
    1. Your MenuViewController class could have a function that changes the image on the button. Eg, func changeButtonMenuImage().
    2. Your 'UISideMenuNavigationController' (or a subclass of it) could have some sort of connection to the MenuViewController either as a property, instance variable or an IBOutlet. Eg, @IBOutlet var menuController: MenuViewController
    3. Then your viewDidAppear and viewDidDisappear can call it's function. Eg, menuController.changeButtonMenuImage()
    0 讨论(0)
提交回复
热议问题