以下は、UIActivityIndicatorを表示しているときにサーバーからデータを取得するために使用しているメソッドです。私は、これらのメソッドをアプリケーションデリゲートに入れて、他のクラスから呼び出しようとしていますが、JSONDataを返す方法はわかりません。誰でもこれを助けることができますか?objective-c:サーバーからデータを返す
-(void)startProcess:(NSString *)buildURL{
UIActivityIndicatorView *aInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActionSheetStyleBlackTranslucent];
[aInd setFrame:CGRectMake(0, 0, 50, 50)];
[aInd startAnimating];
// then call the timeCOnsumingmethod in separate thread.
[NSThread detachNewThreadSelector:@selector(getData:) toTarget:self withObject:buildURL];
}
- (void)getData:(NSString *)buildURL{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Query our database for a restaurant's menus
NSURL *url = [NSURL URLWithString:buildURL];
NSError *e;
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&e];
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
// NSError *error = nil;
[self performSelectorOnMainThread:@selector(endProcess:) withObject:jsonData waitUntilDone:YES];
[pool release];
//return jsonData;
}
- (IBAction)endProcess:(NSData *)jsonData{
// ??????????
return jsonData;
}
Hmm ...畳み込まれた 'detachNewThreadSelector:toTarget:withObject:ではなく' performSelector ... 'を前後に実行するよりはるかに理にかなっています。 NSURLRequestやサードパーティのネットワーキングライブラリを非同期的に使用する例をいくつか紹介します。 [ここにある](http://mobileorchard.com/tutorial-jsonover-http-on-the-iphone/) – FluffulousChimp
このチュートリアルは素晴らしいですが、私は別のクラスにこれらのメソッドを実装するように変更する方法を教えてください? – Apollo