按照類似的問題(不工作)的職務GCDAsyncSocket,我就AppDelegate.h我如何做是在AppDelegate中聲明可用來查看控制器
宣佈GCDAsyncSocket的實例#import <UIKit/UIKit.h>
@class ViewController;
@class GCDAsyncSocket;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
GCDAsyncSocket *asyncSocket;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) GCDAsyncSocket *asyncSocket;
@property (strong, nonatomic) ViewController *viewController;
@end
做插座初始化在AppDelegate.m
#import "AppDelegate.h"
#import "GCDAsyncSocket.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize asyncSocket;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
self.asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];
NSString *host = @"10.1.100.50";
uint16_t port = 3040;
NSError *error = nil;
if (![self.asyncSocket connectToHost:host onPort:port error:&error])
{
NSLog(@"Error connecting: %@", error);
}
char bytes[] = "run";
NSData* requestData = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];
[self.asyncSocket writeData:requestData withTimeout:-1 tag:0];
return YES;
}
的我試圖通過調用訪問來自多個視圖控制器套接字:
GCDAsyncSocket *asyncSocket = [[[UIApplication sharedApplication] delegate] asyncSocket];
代碼完成停止在[[UIApplication sharedApplication]委託]而無法建議asyncSocket。 當在AppDelegate中聲明asyncSocket的實例時,我應該如何在多個視圖控制器中使asyncSocket可訪問?謝謝!
這裏是我的Xcode項目文件:http://bit.ly/PLe1Le