2011-08-30 58 views
0

我想创建一个nsobject,它将拥有我的所有方法,这些方法将用于与我的php脚本交谈并从我的数据库获取数据。nsObject使用ASIHTTPRequests

我想使用ASIHTTPRequest包装为我做这件事,但我不知道如何构造这个nsobject,因为会有几个方法调用不同的PHP脚本(或在不同的功能相同的PHP)。

我在哪里得到升技是在考虑从视图中调用nsobject方法的地方,我想从数据库中显示数据,因为我不知道如何使用 - (void)获取数据到视图)requestFinished:(ASIHTTPRequest *)请求{方法。

例如将NSObject的是这样

//... 
-(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 

或者你把你要显示的信息的视图里面的requestFinished方法?

回答

1

您应该将HTTP请求回调(requestFinished:和requestFailed :)放在UIViewController的子类中。无论视图控制器如何控制相关视图,最有可能的情况是,除非强烈需要在视图控制器之间共享功能,在这种情况下,您可能会将其分解为只有一个作业正在处理这些网络请求的单件。无论哪种方式,需要这些数据的视图都应该被视图控制器修改。视图本身不应该与网络代码有任何联系。

+0

是的,这是有道理的。我对uitableviews的经验有限,我正在越来越好,只是在什么时候以及何处传递数据等等,这让我ab ab不已。 –