I’ve my own class which should do the request and the data processing (parsing). This class should be used from different view controllers. In this class I have implemented:
- (void)sendRequest:(NSString *)url;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;
- (id)parse:(NSString *)aString;
I have created a property called result. If the request arrives, requestFinished is called. In requestFinished the results are saved in result. I thought if I return a value in sendRequest I get the result back. But as I mentioned before requestFinished gets the result and so sendRequest is always returning a nil variable, because at that time the request isn’t finished.
What can I do to return a result? I want that this class can be used from different view controllers. So my first thought creating a method in my view controller and passing the result won’t work.
I read this thread Pass Result of ASIHTTPRequest "requestFinished" Back to Originating Method about using the view controller as delegate. But then I think I have to implement requestFinished and requestFailed in the view controller. The idea of not having duplicate code in different view controllers would be gone away …
Can someone help?
So finally I did it with a delegate.
In my abstraction class (*.h) I defined the following, otherwise you get
warning: no ‘-setDarkness: ‘ method found
warning: (Messages without a matching method signature will be assumed
warning: to return ‘id’ and accept ‘.. .’ as arguments.)
Than I declared an instance variable and some methods in my abstraction class (*.h):
The declaration therefore looks like (*.m):
To pass the result you have to put the following code on requestFailed and requestFinished:
I redefined sendRequest and set the incoming delegate to my instance variable:
In your view controller declare this method:
And I also changed the method call to sendRequest of my abstraction class: