我已经搜索并尝试了很多,但在我的应用程序(使用 iOS 5 sdk)中,只有当我在 viewDidLoad 方法或任何按钮的单击事件中初始化并启动连接时,才会调用 NSURLConnection 委托方法。
但我想在解析我的 JSON 文件后调用它。并为 JSON 中的每条记录创建一个新连接。
谁能告诉我发生了什么?ios sdk 5 有什么变化吗?
这是我的代码:
-(void)getData:(NSString*)URL{
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
shortURL = [NSString stringWithString:URL];
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
//NSAssert(connection!=nil, @"no connection", nil);
[connection start];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
// application specific stuff
}
getData 函数是在继承自 NSObject 的类中编写的。它被称为
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableView 的方法。
请帮我解决一下这个。