selector - iOS Use callback -
please excuse english, i'm french.
so, i'm newbie in ios development. i've developed method must call callback function after async task. i've made search selectors, can't understand how works...
this code :
nsstring *const baseurlstring = @"http://mywebsite.fr/"; + (void) getadverts:(sel *)myselector { // 1 nsurl *baseurl = [nsurl urlwithstring:[nsstring stringwithformat:baseurlstring]]; // 2 afhttpclient *client = [[afhttpclient alloc] initwithbaseurl:baseurl]; [client registerhttpoperationclass:[afjsonrequestoperation class]]; [client setdefaultheader:@"accept" value:@"application/json"]; [client getpath:@"jsoncall" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) { nsdictionary *jsondict = (nsdictionary *) responseobject; nsarray *adverts = [jsondict objectforkey:@"advert"]; nsmutablearray *advertlist = [[nsmutablearray alloc] init]; advert *advert = [[advert alloc] init]; [adverts enumerateobjectsusingblock:^(id obj ,nsuinteger idx, bool *stop){ advert.identifier = [(nsnumber *)[obj objectforkey:@"id"] integervalue]; advert.title = [obj objectforkey:@"title"]; advert.size = [obj objectforkey:@"size"]; nslog(@"id:%d size:%@", advert.identifier, advert.size); [advertlist addobject:advert]; }]; // use selector callback function [self performselector:myselector withobject:@"test"]; } failure:^(afhttprequestoperation *operation, nserror *error) { uialertview *av = [[uialertview alloc] initwithtitle:@"error retrieving weather" message:[nsstring stringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show]; } ]; }
the "caller" code :
- (void)viewdidload { [super viewdidload]; [serverhelper getadverts:@selector(myselector)]; } - (void)myselector { nslog(@"selector ok"); }
here error : unrecognized selector sent class
thanks lot
you aren't quite passing selector correctly , aren't calling properly.
you don't want pass pointer sel
, need sel
. method should be:
+ (void) getadverts:(sel)myselector {
however, @ moment have:
[self performselector:myselector withobject:@"test"];
in instance self
refers class in getadverts
class method has been defined. not instance of class myselector
instance has been declared.
if want way need pass in object want selector called on. getadverts
method needs like:
+ (void) getadvertswithtarget:(id)target selector:(sel)myselector {
and callback using like:
[target performselector:myselector];
and call follows:
[serverhelper getadvertswithtarget:self selector:@selector(myselector)];
Comments
Post a Comment