我如何在其他类的ViewDidAppear和ViewDidDisappear中插入代码 [英] How do I insert code in ViewDidAppear and ViewDidDisappear From other class

查看:39
本文介绍了我如何在其他类的ViewDidAppear和ViewDidDisappear中插入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在出现或消失UISideMenuNavigationController时更改按钮图像.

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

这是一个带有按钮的类.

This is the class that has a button.

class MenuViewController: UIViewController {

  @IBOutlet var btnMenu: UIButton!

  override func viewDidLoad() { 
    super.viewDidLoad()
  }
}

这是我要插入代码的另一个类.

This is the other class that i want to insert code.

open class UISideMenuNavigationController: UINavigationController {

      override open func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)

         // insert some code here but from MenuViewController class
      }

      override open func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        // insert some code here but from MenuViewController class
      }
}

我不想更改UISideMenuNavigationController类,因为它是来自Pod的框架.

I don't want to change UISideMenuNavigationController class because it is framework from pods.

我正在使用 https://github.com/jonkykong/SideMenu 的框架侧菜单

I'm using framework side menu from https://github.com/jonkykong/SideMenu

无论何时出现或出现侧面菜单,我都需要更改按钮图像.我无法从自述文件"侧面菜单中找到方法.这就是为什么我认为需要将代码插入Side Menu类的ViewDidAppear和ViewDidDisappear方法中,但又不想破坏该类的原因.

I need to change button image whenever Side Menu Appear Or Disappear. I can't find the way from ReadMe Side Menu. That's why I think need to insert the code in ViewDidAppear and ViewDidDisappear Method from Side Menu Class but don't want to break the class.

推荐答案

您只需要对 UISideMenuNavigationController 进行子类化,并覆盖 viewDidAppear & viewDidDisappear 方法来调用委托.

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.

您还可以让菜单子类发送 NSNotification ,并让其他有兴趣的对象订阅这些对象.这样,您就可以完全将菜单和其他类分离.

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.

这篇关于我如何在其他类的ViewDidAppear和ViewDidDisappear中插入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆