ios - Passing a variable with prepareForSegue that exists in another function -
i have function:
- (void)requestmewithtoken:(nsstring *)accesstoken { [self.client get:[nsstring stringwithformat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accesstoken] parameters:nil success:^(afhttprequestoperation *operation, nsdictionary *result) { nslog(@"the current user is:%@", result); nsmutabledictionary *newresult = [result mutablecopy]; //copies nsdictionary 'result' nsmutabledictionary called 'newresult' [newresult setobject: [newresult objectforkey: @"id"] forkey: @"appuserid"]; //copies value assigned key 'id', 'appuserid' database can recognize [newresult removeobjectforkey: @"id"]; //removes 'id' key , value nsstring *userid = [newresult objectforkey: @"appuserid"]; //pulls appuserid out nsstring called 'userid' linkedinloginjson *instance= [linkedinloginjson new]; //calls linkedinpost method linkedinloginjson controller [instance linkedinpost:newresult]; [self performseguewithidentifier:@"tohottopics" sender: self]; } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"failed fetch current user %@", error); } ];
}
with code need pass userid
segue next view controller swift
view controller. research seems people suggest prepare segue. how variable exist inside of new function? below tried, after debugging seems prepareforsegue
occurs view created before function executes. examples of prepare segue find online declare , pass variable right there in scope. there way create custom prepareforsegue
of sorts inside of first function? or can somehow variable second function after created in first?
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)userid{ if ([[segue identifier] isequaltostring: @"tohottopics"]) { //create swift file , set property want pass here trendingpollscontroller *vc = (trendingpollscontroller *)segue.destinationviewcontroller; //vc.teststring = blank string in swift file vc.teststring = userid; } }
you need call performsegue within success block, because method asynchronous. way have now, performsegue called before success block runs, userid nil. userid local variable, make available in prepareforsegue method, pass userid instead of self sender argument in performseguewithidentifier:sender:.
Comments
Post a Comment