In a storyboard from the same controller (in the NavigationController hierarchy) I created two different segue and try to determine which of them to call by click and transfer data to the called controller. The transition is carried out correctly, and the data is not transmitted ...

enter image description here

In the CollectionView in the didSelectItemAtIndexPath: method didSelectItemAtIndexPath: I write:

 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { // Если нажал на ФОТО if (indexPath.row > numvideos - 1) { // Если при этом в галерее присутствует ВИДЕО if (numvideos != 0) { NSLog(@"Нажал на фото с индексом: %d", indexPath.row + 1 - numvideos); PhotoFullScreenViewController *photoFullScreenViewController = [[PhotoFullScreenViewController alloc] init]; photoFullScreenViewController.numberOfSelectedPhoto = indexPath.row + 1 - numvideos; photoFullScreenViewController.numberOfTotalPhotos = galleryArray.count - numvideos; [self performSegueWithIdentifier:@"goToPhotoFullScreen" sender:nil]; } // Если ВИДЕО в галерее не присутствует else { NSLog(@"Нажал на фото с индексом: %d", indexPath.row + 1); PhotoFullScreenViewController *photoFullScreenViewController = [PhotoFullScreenViewController new]; photoFullScreenViewController.numberOfSelectedPhoto = indexPath.row + 1; photoFullScreenViewController.numberOfTotalPhotos = galleryArray.count; [self performSegueWithIdentifier:@"goToPhotoFullScreen" sender:nil]; } } // Если нажал на ВИДЕО else { NSLog(@"Нажал на видео с номером: %d", indexPath.row + 1); [self performSegueWithIdentifier:@"goToVideoFullScreen" sender:nil]; } } 

    1 answer 1

    You need to either 1. configure UIViewController in

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"goToPhotoFullScreen"]) { PhotoFullScreenViewController *vc = segue.destinationViewController; // получаем экземпляр из segue NSIndexPath *indexPath = [self.collectionView indexPathForSelectedItem]; // получаем выбранный NSIndexPath if (indexPath) { /* configure your UIViewController with selected indexPath */ } } } 

    In this case, didSelectItemAtIndexPath:

     - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self performSegueWithIdentifier:@"goToPhotoFullScreen" sender:nil]; } 

    or you can configure it in the storyboard so that when you select a cell, a transition is always made (segue)

    1. Another option is to remove the segue, assign your UIViewController storyboard id, this is done in the storyboard, in the tab with the class, just below, let's say "PhotoFullScreen", didSelectItemAtIndexPath: just do the right moment

      PhotoFullScreenViewController vc = [self.storybard instantiateViewControllerWithIdentifier: @ "PhotoFullScreen"]; / configure it here * / [self.navigationController pushViewController: vc animated: true];

    and what you do is you create a new PhotoFullScreenViewController without using a storyboard, configure it, and then ask the storyboard to create it and make the transition. That is, your configuration code ultimately does nothing and the UIViewController is created incorrectly, not in the way you need

    • Thank you, I used the first option. I understood the meaning - I always try to trigger a transition through segue [self performSegueWithIdentifier: @ "goToPhotoFullScreen" sender: nil]; - will be called and - (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender {if ([segue.identifier isEqualToString: @ "goToPhotoFullScreen"]) {... in which I need to transfer data. Right? - Chekist
    • one
      @Chekist prepareForSegue will always be called, even if you configured the transition right in the storyboard from the button or cell. If you want to continue to catch the transition in prepareForSegue, be sure to give it an identifier. The main thing is that you understand the principle. By the way, the second option is no worse than the first, except that it is less visual in a storyboard, but it is often more convenient to use it - iFreeman