不从HomeViewController类调用MFSideMenu委托方法

时间:2018-02-27 06:41:35

标签: ios objective-c delegates

我在我的应用程序中实现了MFSideMenu以显示左侧幻灯片输入菜单。现在我想通过在ThirdParty类MFSideMenuContainerViewController.h中创建委托方法来识别滑入和滑出(即Sidemenu打开或关闭状态)

#import <UIKit/UIKit.h>
#import "MFSideMenuShadow.h"

@class MFSideMenuContainerViewController;
@protocol MFSideMenuContainerViewControllerViewDelegate <NSObject>   //define delegate protocol

- (void)swipedLeftSidemenu:(BOOL)isOpen;  //define delegate method to be implemented within another class
@end //end protocol

@interface MFSideMenuContainerViewController : UIViewController<UIGestureRecognizerDelegate>

//custom delegate property
@property (nonatomic, weak) id <MFSideMenuContainerViewControllerViewDelegate> delegate1; 
@end

然后从MFSideMenuContainerViewController.m的下面方法调用委托方法

- (void)openLeftSideMenuCompletion:(void (^)(void))completion
{
   if(!self.leftMenuViewController) return;

   //call as below 
   [self.delegate1 swipedLeftSidemenu:YES];

   //below is default thirdparty implementation
   [self.menuContainerView bringSubviewToFront:[self.leftMenuViewController view]];
   [self setCenterViewControllerOffset:self.leftMenuWidth animated:YES completion:completion];

 }

另一个viewcontroller类中使用的委托方法检测侧面菜单是打开还是关闭,因为我在代码下面实现了

在DemoViewController.h文件中

#import "MFSideMenuContainerViewController.h"
@interface DemoViewController : UIViewController <ContactViewDelegate,MFSideMenuContainerViewControllerViewDelegate> 
在DemoViewController.m文件中

- (void)viewDidLoad
{
  [super viewDidLoad];

   MFSideMenuContainerViewController *vc2 = [[MFSideMenuContainerViewController alloc] init];
   vc2.delegate1 = self;
}

//DelegateMethod Implementation
-(void)swipedLeftSidemenu:(BOOL)isOpen
{
   if(isOpen)
   {
     // code is here if side menu is open by swiping right
   }
   else
   {
     // code is here if side menu is closed by swiping left
   }
}

现在我遇到问题是DemoViewController.m文件中的上述方法永远不会被调用虽然sidemenu可以向左或向右滑动。任何一个指南如何检测侧面菜单是从DemoViewController类打开还是关闭?或者为什么从未调用此方法?

我的appdelegate.m文件只有以下代码

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    - (DemoViewController *)demoController
    {
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

       DemoViewController *demoController = [storyboard instantiateViewControllerWithIdentifier:@"DemoViewController"];

       return demoController;
    }

    - (UINavigationController *)navigationController
    {
       return [[UINavigationController alloc]
        initWithRootViewController:[self demoController]];
    }

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

       SideMenuViewController *leftMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"SideMenuViewController"];

       MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                containerWithCenterViewController:[self navigationController]
                                                leftMenuViewController:leftMenuViewController
                                                rightMenuViewController:nil];

      self.window.rootViewController = container;
      [self.window makeKeyAndVisible];

      return YES;
   }

2 个答案:

答案 0 :(得分:0)

在AppDelegate.h文件中更新代码

@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic)ViewController *viewController;
@property(strong,nonatomic)LeftSideViewController *leftSideViewController;
@property(strong,nonatomic)MFSideMenuContainerViewController *container;
@property(strong,nonatomic)UINavigationController *navigationController;

@end

并在AppDelegate.m文件中更新您的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.container = [MFSideMenuContainerViewController containerWithCenterViewController:self.navigationController leftMenuViewController:_leftSideViewController rightMenuViewController:nil];


    self.window setRootViewController:_container];
    [self.window makeKeyAndVisible];
    return YES;
}

答案 1 :(得分:0)

我用NotoficationCenter Observer方法替换了委托方法,代码如下:

STEP-1

在ThirdParty类MFSideMenuContainerViewController.m中作为

- (void)openLeftSideMenuCompletion:(void (^)(void))completion
{
   if(!self.leftMenuViewController) return;
   NSDictionary* userInfo = @{@"isOpen": @"1"};
   [[NSNotificationCenter defaultCenter] postNotificationName:@"swipeClassDelegateMethod" object:nil userInfo:userInfo];

   //below is default thirdparty implementation

   [self.menuContainerView bringSubviewToFront:[self.leftMenuViewController view]];
   [self setCenterViewControllerOffset:self.leftMenuWidth animated:YES completion:completion];
}

第2步:然后我从我的DemoViewController.m文件中调用了以下代码viewDidLoad方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swipeClassDelegateMethod :) name:@&#34; swipeClassDelegateMethod&#34;对象:无];

第3步:最后我在相同的DemoViewController.m类中使用必要的代码实现了这个函数

- (无效)swipeClassDelegateMethod:(NSNotification *)通知 { }