委托模式ios

时间:2015-09-06 22:16:49

标签: ios objective-c delegates

我一直在关注这个例子,以帮助我建立一个代表但不幸的是我错过了一些东西,所以它不适合我。 How do I set up a simple delegate to communicate between two view controllers?

我的代码如下所示:

//  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

/ *根据下面的建议,我在这里更改了代码/ *

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

然后在Controller类中我有以下内容:

//
//  GameLogicController.h

#import "HintsViewController.h"

@interface GameLogicController : NSObject < HintDelegateProtocol>

@end

在实施中,我有以下内容:

//  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");
}

使用断点我可以看到正在调用HintsViewController中的IBActions,但是在gameLogicController中的任何委托方法中放置断点都不会被命中。所以我错过了设置GameLogicController和HintsViewController之间连接的重要一步。谁能帮我发现它?

1 个答案:

答案 0 :(得分:0)

假设您有两个文件:一个是您的ViewController,另一个是您的ConnectionManager类。

在ConnectionManager类中声明协议及其方法,并在ViewController类中定义协议方法。通过在ViewController类中设置ConnectionManager类的委托,可以调用Protocol方法。

@protocol ConnManagerDelegate<NSObject>

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

@end

@interface ConnectionManager : NSObject<NSURLConnectionDelegate>

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

并且在同一个文件.m中,当你的回复刚来电时

[Self.delegate didReceiveData:mDict];

在分配init ConnectionManager类之后的ViewController文件中,将其委托设置为self并定义协议方法。通过这些方法,您将获得ConnectionManager类的响应。

这是所有协议委派模式