我的应用程序和协议有问题。我无法发布图片(...),但我的 Storyboard 包含两个控制器。首先是带有 UIButton 的主菜单控制器。当用户按下按钮时,会出现一个表格视图,其控制器为“可用服务器控制器”。
好吧,主菜单控制器公开了以下协议:
#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
……
还有他们
#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 变得不可用,但相应的委托函数在表视图控制器中永远不会被调用。为什么??
使用委托和协议模式有什么问题吗?