开发者

nsObject for using ASIHTTPRequests

开发者 https://www.devze.com 2023-03-31 16:36 出处:网络
I would like to create an nsobject that will have all of my methods that will be 开发者_运维百科used for talking to my php scripts and getting data from my database.

I would like to create an nsobject that will have all of my methods that will be 开发者_运维百科used for talking to my php scripts and getting data from my database.

I would like to use ASIHTTPRequest wrapper to do this for me, however I am not sure how to structure this nsobject as there will be several methods calling different php scripts (or the same php with different functions in that).

Where I get abit lost is when thinking about calling an nsobject method from the view where I would like to display the data from the database as I am not sure how to get the data over to the view with the - (void)requestFinished:(ASIHTTPRequest *)request{ method.

For instance would the nsobject be like this

//...
-(IBAction) method1
{
     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setDelegate:self];
     [request startAsynchronous];
}

-(IBAction) method2
{
     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setDelegate:self];
     [request startAsynchronous];
}
-(IBAction) method3
{
     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setDelegate:self];
     [request startAsynchronous];
}

//Then catch each method request with one

- (void)requestFinished:(ASIHTTPRequest *)request{ //etc

//and use one failed request 

- (void)requestFailed:(ASIHTTPRequest *)request{ //etc

or do you put the requestFinished method inside the view you want to display the information in?


You should probably be putting the HTTP request callbacks (requestFinished: and requestFailed:) in a subclass of UIViewController. Whatever view controller is controlling the relevant view, most likely, unless there is a strong need to share the functionality between view controllers, in which case you may factor it into a singleton whose only job is handling these network requests. Either way, the views that need this data should be modified by their view controllers. The view itself shouldn't have any contact with network code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号