Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

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

self.mainSlideMenu.slideMenuDelegate = self;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top