Я новичок в IOS и пытаюсь понять это.prepareForSegue Выполняется перед действием кнопки
Я пытаюсь передать данные json из одного контроллера View в другой (TableViewcontroller), и я хочу, чтобы действие кнопки (это данные json) выполнялось до метода prepareForSegue (который затем передает данные json, которые я получил от действие кнопки) в TableViewcontroller.
Но мой метод prepareseue выполняется сначала до нажатия кнопки, а tableview не получает данных?
Но когда я нажимаю кнопку во второй раз, я получаю данные в формате tableview json.
Это происходит, когда я пытаюсь передать Textfield в URl, если я не использую прямой url.
Может кто-нибудь Пожалуйста, помогите мне с этим. Также дайте мне знать, если есть какой-либо другой способ сделать это.
Вот код в первом VC:
- (IBAction)buttonShow:(id)sender {
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [NSString stringWithFormat:@"Some API URL=%@",self.ingredientTextfield.text];
NSURL *url = [[NSURL alloc]initWithString:urlString];
req = [[NSMutableURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *datatask = [session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *rcCount = [json objectForKey:@"count"];
NSLog(@"Recipe Count : %@",rcCount);
NSMutableDictionary *recipedata = [json objectForKey:@"recipes"];
NSLog(@"Recipe data : %@",recipedata);
NSMutableArray *titlearray = [[NSMutableArray alloc]init];
for (NSMutableDictionary *singlerecipedata in recipedata) {
NSString *title = [singlerecipedata objectForKey:@"title"];
NSLog(@"Recipe title : %@",title);
[titlearray addObject:title];
NSLog(@"tittlearray : %@",titlearray);
}
actualDataArray = [[NSArray alloc]initWithArray:titlearray];
NSLog(@"tittle array inside for:%@",actualDataArray);
}];
NSLog(@"tittle array outside :%@",actualDataArray);
[datatask resume];
[self performSegueWithIdentifier:@"segueIdentifier" sender:sender];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"segueIdentifier"]) {
RecipesTableViewController *new = [segue destinationViewController];
NSLog(@"in segue :%@",actualDataArray);
[new setMyrecipeTittleArray:actualDataArray];
}
}
Вы можете указать код? –