Reputation: 377
I have this 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 the NSDictionary 'result' to a NSMutableDictionary called 'newResult'
[newResult setObject: [newResult objectForKey: @"id"] forKey: @"AppUserID"]; //copies the value assigned to key 'id', to 'AppUserID' so the database can recognize it
[newResult removeObjectForKey: @"id"]; //removes 'id' key and value
NSString *userID = [newResult objectForKey: @"AppUserID"]; //pulls the AppUserID out as a NSString called 'userID'
LinkedInLoginJSON *instance= [LinkedInLoginJSON new]; //calls LinkedInPost method from LinkedInLoginJSON controller
[instance LinkedInPost:newResult];
[self performSegueWithIdentifier:@"toHotTopics" sender: self];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"failed to fetch current user %@", error);
}
];
}
With this code I need to pass userid
with this segue to my next view controller which is a swift
view controller. From research it seems people suggest prepare for segue. How do I get my variable to exist inside of this new function? Below is what I tried, but after debugging it seems prepareForSegue
actually occurs as soon as the view is created before my function executes. All the examples of prepare for segue I could find online just declare and pass a variable right there in scope. Is there a way to create a custom prepareForSegue
of some sorts inside of my first function? Or can I somehow get my variable to my second function after it is created in the first?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)userID{
if ([[segue identifier] isEqualToString: @"toHotTopics"]) {
//create the swift file and set the property I want to pass here
TrendingPollsController *vc = (TrendingPollsController *)segue.destinationViewController;
//vc.teststring = blank string in my swift file
vc.teststring = userID;
}
}
Upvotes: 1
Views: 483
Reputation: 104092
You need to call performSegue from within the success block, because the method is asynchronous. The way you have it now, performSegue is called before the success block runs, so userID will be nil. Also userID is a local variable, so to make it available in the prepareForSegue method, pass userID instead of self as the sender argument in performSegueWithIdentifier:sender:.
Upvotes: 1