简体   繁体   English

如何在iOS中使用委派的滑出菜单?

[英]How do I use delegation in iOS for slide out menu?

I am trying to figure out delegation in iOS. 我试图找出在iOS中的委派。 Basically, I have classA which contains methodA . 基本上,我有classA其中包含methodA I also have classB which I would like to call methodA from. 我也有classB我想调用methodA的。

To be specific, I have a class called ViewControllerRootHome and class called ViewControllerRootHomeLeftPanel . 具体而言,我有一类叫做ViewControllerRootHome和类称为ViewControllerRootHomeLeftPanel The ViewControllerRootHome has a method in it called, movePanelToOriginalPosition I would like to call this method from the ViewControllerRootHomeLeftPanel class. ViewControllerRootHome有一个名为movePanelToOriginalPosition的方法,我想从ViewControllerRootHomeLeftPanel类中调用此方法。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Ohh forgot to mention I'm still using Objective-C for the project. 哦,忘了提及我在该项目中仍在使用Objective-C。

I'll give this an attempt. 我会尝试的。

Let's say you've got a ViewController called ViewControllerA , and another ViewController called ViewControllerB . 假设您有一个名为ViewControllerA的ViewController和另一个名为ViewControllerB的ViewController We want to call a method inside A from B . 我们想从B调用A内部的方法。 How are we going to achieve this? 我们将如何实现这一目标?

Simple. 简单。 We will define a protocol inside B that A will comply to. 我们将在B内部定义一个A会遵守的协议。 Let me do that right here. 让我在这里做。

#import ...

@protocol myProtocol; // Declare Protocol

@interface ViewControllerB : UIViewController

@property (nonatomic, weak)id <myProtocol> myDelegate; // Create Delegate property

@end // Notice this is AFTER the @end of the @interface declaration

@protocol myProtocol <NSObject> // Define Protocol
-(void)doSomething;
@end

Okay, now you have defined a protocol called myProtocol that you wish to use inside ViewControllerA 好的,现在您已经定义了一个希望在ViewControllerA中使用的协议myProtocol

Let us use it there. 让我们在那里使用它。 We will have to do several things: One, comply to the protocol. 我们将不得不做几件事:一,遵守协议。 And two, set our current VC as it's delegate! 第二,将当前的VC设置为委托!

#import ...
#import "ViewControllerB" // IMPORT the VC with the Protocol

@interface ViewControllerA : UIViewController <myProtocol> // Conform to Protocl

@property (nonatomic)ViewControllerB *viewControllerB;

@end

Notice I've defined a property of type ViewControllerB. 注意,我已经定义了ViewControllerB类型的属性。 You will need to have a reference to ViewControllerB in some shape or form. 您将需要以某种形状或形式引用ViewControllerB。 This is usually easy to achieve because you normally create an instance of ViewControllerB from ViewControllerA. 这通常很容易实现,因为通常是从ViewControllerA创建ViewControllerB的实例。 Otherwise it will need to be set externally or passed to ViewControllerA upon initialization and you set it as a property there. 否则,将需要在外部进行设置或在初始化时将其传递给ViewControllerA,然后在此处将其设置为属性。

Inside ViewControllerA.m, set ViewControllerA as it's delegate : 在ViewControllerA.m中,将ViewControllerA设置为委托

self.ViewControllerB.myDelegate = self;

Now, all you have to do is define the method from the protocol inside ViewController A so it can be called: 现在,您所要做的就是从ViewController A内部的协议中定义方法,以便可以将其调用:

-(void)doSomething
{
...
}

This is all you need to do. 这就是您需要做的。 However, please note that if you have TWO ViewControllers complying to each other's protocols , you will likely have to declare the protocols inside their own header files. 但是,请注意, 如果您有两个遵守彼此协议的ViewController ,则可能必须在其自己的头文件中声明协议。

Edit: How to call the method. 编辑:如何调用该方法。 If you want to call the method defined inside the protocol. 如果要调用协议内部定义的方法。 You will do so inside ViewControllerB, like so: 您将在ViewControllerB中这样做,如下所示:

    if ([self.myDelegate respondsToSelector:@selector(doSomething)])
    {
        [self.myDelegate doSomething];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM