There are two ways around this.
- Create Delegate Interface
- Use blocks
- (.. ). , , , .
, :
1.
, , , . , :
:
@protocol RequestClassDelegate <NSObject>
- (void)requestCompleted:(ResponseClass *)data;
- (void)requestError:(NSError *)error;
@end
, , :
@interface RequestClass : NSObject
- (void)makeRequest:(id<RequestClassDelegate>)delegate;
@end
, :
@implementation RequestClass
{
__weak id<RequestClassDelegate> _delegate;
}
- (void)makeRequest:(id<RequestClassDelegate>)delegate
{
_delegate = delegate;
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
[_delegate requestCompleted:theResult];
}
@end
2.
, , , . RequestClass :
typedef void (^requestCompletedBlock)(id);
typedef void (^requestErrorBlock)(NSError *);
@interface RequestClass : NSObject
@property (nonatomic, copy) requestCompletedBlock completed;
@property (nonatomic, copy) requestErrorBlock errored;
- (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error;
@end
:
@implementation RequestClass
@synthesize completed = _completed;
@synthesize errored = _errored;
- (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error
{
self.completed = completed;
self.errored = error;
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
self.completed(theResult);
}
@end