简体   繁体   中英

Delegate Pattern ios

I've been following this example to help me build a delegate but unfortunately I've missed something so it is not working for me. How do I set up a simple delegate to communicate between two view controllers?

My code looks like this:

//  HintsViewController.h

#import <UIKit/UIKit.h>

@protocol HintDelegateProtocol;

@interface HintsViewController : UIViewController

@property (weak, nonatomic) id<HintDelegateProtocol> hintDelegate;

-(IBAction)showFirstLetter:(id)sender
-(IBAction)showHint:(id)sender;
-(IBAction)showAnswer:(id)sender;

@end

@protocol HintDelegateProtocol <NSObject>

-(void)HintsViewController:(HintsViewController*)hintsViewController
                showFirstLetter:(NSString*)firstLetter;


-(void)HintsViewController:(HintsViewController*)hintsViewController
           showHint:(NSString*)hint;


-(void)HintsViewController:(HintsViewController*)hintsViewController
           showAnswer:(NSString*)answer;

@end

//
//  HintsViewController.m

#import "HintsViewController.h"

@implementation HintsViewController

#pragma mark -
#pragma mark IBActions

/* As per a suggestion below I changed the code here /*

- (IBAction)showHint:(id)sender
{
     [self.hintDelegate HintsViewController:self showHint:@"Hint"];
}

- (IBAction)showFirstLetter:(id)sender
{
   [self.hintDelegate HintsViewController:self showFirstLetter:@"FirstLetter"];

}

- (IBAction)showAnswer:(id)sender
{
    [self.hintDelegate HintsViewController:self showAnswer:@"Answer"];
}

@end

And then in the a Controller class I have the following:

//
//  GameLogicController.h

#import "HintsViewController.h"

@interface GameLogicController : NSObject < HintDelegateProtocol>

@end

And in the implementation I have the following:

//  GameLogicController.m
-(void) nextRiddle 
{
        HintsViewController *hintsViewController = [[HintsViewController alloc] init];
        hintsViewController.hintDelegate = self;
}

#pragma mark -
#pragma mark HintsFunctionality

-(void)HintsViewController:(HintsViewController*)hintsViewController
           showFirstLetter:(NSString*)firstLetter
{
    NSLog(@"Show First Letter called");
}


-(void)HintsViewController:(HintsViewController*)hintsViewController
                  showHint:(NSString*)hint
{
    NSLog(@"show Hint called");
}


-(void)HintsViewController:(HintsViewController*)hintsViewController
                showAnswer:(NSString*)answer
{
     NSLog(@"Show answer called");
}

Using breakpoints I can see that the IBActions in the HintsViewController are being called, but putting a breakpoint in any of the delegate methods in the gameLogicController are never hit. So I have missed an important step in setting up the connection between the GameLogicController and the HintsViewController. Can anyone help me spot it?

Say you have two files: one is your ViewController, and other is your ConnectionManager Class.

Declare protocol and its methods in your ConnectionManager class, and define your protocol methods in the ViewController class. By setting the delegate of your ConnectionManager class in ViewController Class, you can call your Protocol method.

@protocol ConnManagerDelegate<NSObject>

- (void)didReceiveData:(NSDictionary *)data;
- (void)didFailWithError:(NSError*)error;

@end

@interface ConnectionManager : NSObject<NSURLConnectionDelegate>

@property(nonatomic,assign)id< ConnManagerDelegate > delegate;

And elseswhere in the same file .m, when your response comes just call

[Self.delegate didReceiveData:mDict];

In the ViewController file after you alloc init ConnectionManager class, set its delegate to self and define the protocol methods. It is these methods you will have your response from ConnectionManager class.

This is all Protocol Delegation pattern

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