作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
<分区>
为了在我的多个 View 上建立网络连接,我创建了一个单例网络 Controller 来处理服务器和客户端之间的数据。不幸的是,它不起作用,因为委托(delegate)方法没有从我的单例调用到另一个 View 。在我的代码下面:
** 单例是 SocketIOConnection.h 和 .m
//
// SocketIOConnection.h
#import <Foundation/Foundation.h>
#import "SocketIO.h"
#import "SocketIOPacket.h"
@protocol SocketIOConnectionDelegate <NSObject>
@required
- (void) receivedPacket:(id)packet;
@end
@interface SocketIOConnection : NSObject <SocketIODelegate> {
SocketIO *IO;
id <SocketIOConnectionDelegate> delegate;
}
@property (nonatomic, retain) IBOutlet SocketIO *IO;
@property (retain) id <SocketIOConnectionDelegate> delegate;
+ (SocketIOConnection *)sharedSingleton;
@end
//
// SocketIOConnection.m
#import "SocketIOConnection.h"
@implementation SocketIOConnection
@synthesize IO, delegate;
static SocketIOConnection *shared = NULL;
-(id)init {
if (self = [super init]) {
IO = [[SocketIO alloc] initWithDelegate:self];
[IO connectToHost:@"domain.com" onPort:443];
}
return self;
}
+ (SocketIOConnection *)sharedSingleton
{
@synchronized(shared)
{
if ( !shared || shared == NULL )
{
// allocate the shared instance, because it hasn't been done yet
shared = [[SocketIOConnection alloc] init];
}
return shared;
}
}
-(void)socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
NSLog(@"Delegating received packet..");
[delegate receivedPacket:packet.dataAsJSON];
}
@end
所以这是我的单例代码,下面我将发布我的 viewcontroller.h 和 .m 的代码
//
// ViewController.h
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#import "SocketIOConnection.h"
@interface ViewController : UIViewController <MBProgressHUDDelegate, SocketIOConnectionDelegate>
{
MBProgressHUD *HUD;
}
@property (nonatomic, retain) SocketIOConnection *IOConnection;
@end
//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
NSDictionary *operatorData;
}
@synthesize SESSION, IOConnection;
#pragma mark - view Did Load, View Will Appear & didReceiveMemoryWarning
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated {
IOConnection = [SocketIOConnection sharedSingleton];
// fire auth function to verify the operator and sign him in.
NSMutableDictionary *tokenAndLicense = [[NSMutableDictionary alloc] init];
[tokenAndLicense setValue:[operatorData objectForKey:@"token"] forKey:@"__ca.token"];
[tokenAndLicense setValue:[operatorData objectForKey:@"license"] forKey:@"__ca.license"];
[IOConnection.IO sendEvent:@"auth" withData:tokenAndLicense];
}
-(void)receivedPacket:(id)packet { <<<< void that should be fired because of the delegate
NSLog(@"Receive ieks..");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
为什么我的委托(delegate)方法没有被调用?
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!