如何在 iPhone 中使用多个代表?
我正在使用下面的代码来使用 json 但我需要在同一页面中更多的 url 连接,如何实现它,提前感谢
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}
- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];
}
-(void)searchForStuff:(NSString *)text
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
我正在使用 php 进行网络访问
I am using below code to use json but i need more url connection in same page, how to achive it, thanks in advance
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}
- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];
}
-(void)searchForStuff:(NSString *)text
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
I am using php for web access
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要做任何那样的事情。
相反,使用出色的 ASIHTTPRequest 库,它使一切变得更加简单和更好。从字面上看,自从我几年前发现 ASI 以来,我还没有编写过任何 NSURLConnection,一个也没有。
ASI 的块接口允许您在触发请求对象之前使用其处理程序代码配置请求对象,并且无需任何委派。
如果块让您感到害怕,您还可以将特定请求指向特定方法,以便可以单独处理不同的请求类型:
Don't do any of that.
Instead use the brilliant ASIHTTPRequest library, which makes everything much simpler and better. Literally, since I discovered ASI a couple years ago, I haven't written a single NSURLConnection, not one.
ASI's block interface lets you configure a request object with its handler code before firing it, and does away with any need for delegation.
If blocks scare you, you can also point a particular request at a particular method, so different request types can be handled separately:
您实际上不需要超过一名代表。您需要多个 NSURLConnection,您可以测试看看哪一个正在调用委托方法。
例如。假设以下实例变量(或属性):
首先实例化每个 NSURLConnection 变量
然后您可以测试以查看哪个连接正在调用委托方法并根据连接自定义实现
您需要为每个委托方法执行此操作。
You don't actually need more than one delegate. You need more than one NSURLConnection and you can test to see which one is calling the delegate method.
For example. Assuming the following instance variable (or properties):
First you instantiate each NSURLConnection variable
Then you can test to see which connection is calling the delegate method and customize the implementation based on the connection
You'll need to do this for each delegate method.
您可以使用实例变量来保存指向连接的指针。然后在委托回调中,检查指针是否相等,以检查您正在处理哪个连接。
You could use instance variables to keep pointers to the connections. Then in the delegate callbacks, check for pointer equality to check which connection you're dealing with.
由于 NSValue 符合 NSCopying 规范,我使用它来包装指向连接的指针,并将其用作从 NSMutableDictionary 访问相关数据的密钥。例如,您可能会执行以下操作:
As NSValue conforms to
NSCopying
I use it to wrap the pointer to the connection, and use this as the key to access relevant data from aNSMutableDictionary
. For example you might do something like the following: