セレクターを別のクラスに送信して、その別のクラスで実行することは可能ですか?
これはエラーでクラッシュするよう*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[webManager _selector]: unrecognized selector sent to instance
です。これが不可能な場合、代替手段として何をお勧めしますか?
メソッドは、実行される順序で配置されます。
//in HomeViewController
-(void)viewDidLoad
{
WebManager *webManager = [[WebManager alloc] init];
URLCreator *urlCreator = [[URLCreator alloc] initWithParam:@"default"];
[webManager load:[urlCreator createURL] withSelector:@selector(testSelector)];
}
//in WebManager, which is also the delegate for the URL it loads when the load method is called...
-(void)load:(NSString*)url withSelector:(SEL)selector
{
_selector = selector;
[self load:url];
}
-(void)load:(NSString*)url{
NSURLRequest * request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
//now the delegate response, ALSO in WebManager
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Connection did finish loading. Parsing the JSON");
[self getJSONDict:rawData]; //parses the data to a dictionary
//perform the selector that is requested if there is one included
if (_selector)
{
NSLog(@"Performing selector: %@", NSStringFromSelector(_selector));
//the selector is called testSelector
[self performSelector:@selector(_selector)];
}
}
- (void)testSelector //also in the WebManager class
{
NSLog(@"Selector worked!");
}