IOS AMSlideMenu open menu callback

泄露秘密 提交于 2019-12-11 06:23:44

问题


I'm new in IOS, and can't understand some things about delegates and protocols. In my project I'm using AMSlideMenu for slide menu. I need to update same data in slide menu. For that I need to catch openMenu callback. In instruction for slide menu I found this

 If you want to get menu's open/close callbacks, then set MainVC's delegate property, and implement protocol named 'AMSlideMenuProtocols'. 

 @optional
 - (void)leftMenuWillOpen;
 - (void)leftMenuDidOpen;
 - (void)rightMenuWillOpen;
 - (void)rightMenuDidOpen;

I tried to catch callback with this code

//myClass.h    
#import "AMSlideMenuLeftTableViewController.h"
#import "AMSlideMenuMainViewController.h"
@interface myClass : AMSlideMenuLeftTableViewController<AMSlideMenuDelegate>

@end

//myClass.m
#import "myClass.h"

@interface myClass ()

@end

@implementation myClass
-(void)leftMenuWillOpen
{
    //something
}
@end

but leftMenuWillOpen never gets invoked. What I did wrong?


回答1:


I guess you are not setting the delegate to the slide menu. The object of myClass should be the delegate to AMSlideMenu object.




回答2:


To complete the answer and for a quick reference, you need to add this to your viewDidLoad :

self.mainSlideMenu.slideMenuDelegate = self;



回答3:


Swift 2.0 compatible
This is my case, but I guess is the general situation:

class MainViewController: AMSlideMenuMainViewController, AMSlideMenuDelegate  {

  override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }

  required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    self.leftMenu = LeftMenuViewController(nibName:"LeftMenuViewController", bundle:nil)
    self.rightMenu = nil
    self.view.backgroundColor = UIColor.whiteColor()
    self.slideMenuDelegate = self /* FOCUS HERE */
  }

  // MARK: - AMSlideMenuDelegate -

  func leftMenuWillOpen() {
    let menu = self.leftMenu as! LeftMenuViewController
    menu.centerMenuItems()
  }
}

Where LeftMenuViewController is a custom class that inherits from AMSlideMenuLeftTableViewController.



来源:https://stackoverflow.com/questions/22981470/ios-amslidemenu-open-menu-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!