简体   繁体   中英

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

I am trying to figure out delegation in iOS. Basically, I have classA which contains methodA . I also have classB which I would like to call methodA from.

To be specific, I have a class called ViewControllerRootHome and class called ViewControllerRootHomeLeftPanel . The ViewControllerRootHome has a method in it called, movePanelToOriginalPosition I would like to call this method from the ViewControllerRootHomeLeftPanel class.

Any help would be greatly appreciated. Ohh forgot to mention I'm still using Objective-C for the project.

I'll give this an attempt.

Let's say you've got a ViewController called ViewControllerA , and another ViewController called ViewControllerB . We want to call a method inside A from B . How are we going to achieve this?

Simple. We will define a protocol inside B that A will comply to. 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

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!

#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. You will need to have a reference to ViewControllerB in some shape or form. This is usually easy to achieve because you normally create an instance of ViewControllerB from ViewControllerA. Otherwise it will need to be set externally or passed to ViewControllerA upon initialization and you set it as a property there.

Inside ViewControllerA.m, set ViewControllerA as it's delegate :

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:

-(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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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