不从HomeViewController类调用MFSideMenu委托方法

问题描述 投票:0回答:2

我已经在我的应用程序中实现了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;
   }
ios objective-c delegates
2个回答
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;
}

0
投票

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

步骤1

在第三方类MFSideMenu ContainerViewController.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:@“swipeClassDelegateMethod”object:nil];

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

- (void)swipeClassDelegateMethod:(NSNotification *)notification {}

© www.soinside.com 2019 - 2024. All rights reserved.