私の.hファイル:
#import <UIKit/UIKit.h>
#import "ServiceConnector.h"
@interface PartnerTableController : UITableViewController <ServiceConnectorDelegate>
@property (nonatomic, retain) IBOutlet NSMutableArray *allPartners;
- (IBAction)download:(id)sender;
@end
私の.mファイルの一部:
- (void)viewDidLoad
{
[super viewDidLoad];
[self download:self];
}
...
- (IBAction)download:(id)sender { //perform get request
ServiceConnector *serviceConnector = [[ServiceConnector alloc] init];
serviceConnector.delegate = self;
[serviceConnector download];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"count allpartners: %d", [allPartners count]);
return [allPartners count];
}
問題はnumberOfRowsInSection
、JSON-Stringをダウンロードし、オブジェクトを作成してNSMutableArrayに配置するのに時間がかかるため、0が返されることですallPartners
。どういうわけかメソッドをブロックできますdownload
か?
更新:allPartners
次の方法で入力されます:
[serviceConnector download];
-(void)download{
//build up the request that is to be sent to the server
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.bala.com"]];
[request setHTTPMethod:@"GET"];
[request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; //selects what task the server will perform
//initialize an NSURLConnection with the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
NSLog(@"Connection Failed");
}
}