Objective-C iOS上的协议

时间:2012-10-14 16:55:01

标签: iphone ios delegates protocols

我的应用和协议存在问题。我无法发布图像(...)但我的Storyboard包含两个控制器。首先是带有UIButton的主菜单控制器。当用户按下该按钮时,将显示一个表视图,其控制器为“可用服务器控制器”。

好吧,Main-Menu控制器公开了以下协议:

#import <UIKit/UIKit.h>
#import "Client.h"
#import "MyRstAaAppDelegate.h"

@class MyRstAaClientViewController;

@protocol ClientNetworkDelegate <NSObject>
-(void)serverBecameAvaiable:(NSString *)peerID;
-(void)serverBecameUnavailable:(NSString *)peerID;
-(void)didConnectToServer:(NSString *)peerID;
-(void)didDisconnectFromServer:(NSString *)peerID;
@end

@interface MyRstAaClientViewController : UIViewController <ClientDelegate>

#pragma PROPERTIES
//Delegate property
@property (nonatomic, weak) id<ClientNetworkDelegate> delegate;

@end

可用服务器表视图控制器实现协议的方法来重新加载表数据和执行其他操作。这是它的代码:

#import <UIKit/UIKit.h>
#import "MyRstAaClientViewController.h"

@interface MyRstAaClientServerListTableViewController : UITableViewController <ClientNetworkDelegate>

@end

... ...

而.m

#import "MyRstAaClientServerListTableViewController.h"

@interface MyRstAaClientServerListTableViewController ()

//Client shared istance
@property (nonatomic, strong) Client *client;
@property (nonatomic, strong) MyRstAaClientViewController *mainController;
@end

@implementation MyRstAaClientServerListTableViewController

#pragma PROPERTIES
@synthesize client = _client;
@synthesize mainController = _mainController;

#pragma INITIALIZATION FUNCTIONS
- (void)viewDidLoad
{
    [super viewDidLoad];
    MyRstAaAppDelegate *appDelegate = (MyRstAaAppDelegate *)[[UIApplication sharedApplication]delegate];
    self.client = appDelegate.client;

    if(_mainController == nil)
        _mainController = [[MyRstAaClientViewController alloc]init];
    [self.mainController setDelegate:self];

    [self.tableView reloadData];
}

#pragma DELEGATES
-(void)serverBecameAvaiable:(NSString *)peerID;
{
    //Reload table data
    [self.tableView reloadData];

#ifdef DEBUG
    NSLog(@"CLIENT-SERVERLIST:: Server become Available");
#endif
}
-(void)serverBecameUnavailable:(NSString *)peerID;
{
    //Reload table data
    [self.tableView reloadData];

    #ifdef DEBUG
    NSLog(@"CLIENT-SERVERLIST:: Server become unavaiable");
    #endif
}
- (void)didConnectToServer:(NSString *)peerID
{
    #ifdef DEBUG
    NSLog(@"CLIENT-SERVERLIST:: Server become Available");
    #endif
}
- (void)didDisconnectFromServer:(NSString *)peerID
{
    #ifdef DEBUG
    NSLog(@"CLIENT-SERVERLIST:: did Disconnect From Server");
    #endif
}

这是我的应用程序的MainMenu控制器的.m文件中委托调用的示例

#import "MyRstAaClientViewController.h"

@interface MyRstAaClientViewController ()

@property (nonatomic, strong) Client *client;

@end

@implementation MyRstAaClientViewController

#pragma PROPERTIES
@synthesize client = _client;
@synthesize delegate = _delegate;

-(void)Client:(Client *)client serverBecameUnavailable:(NSString *)peerID
{
#ifdef DEBUG
    NSLog(@"CLIENT-HOME:: Server become unavaiable");
#endif

    //Call delegate methodå
    [self.delegate serverBecameUnavailable:peerID];
}

问题在于,当我在iPhone上运行我的应用程序,并且进入“可用服务器”列表视图时,我可以看到,例如,在输出上的字符串: CLIENT-HOME :: Server成为unavaiable 但从不调用表视图控制器中相应的委托函数。为什么?

使用委托和协议模式有什么问题吗?

0 个答案:

没有答案