2

I've been having trouble getting data to pass from a detail view controller to the master one. The hierarchy of the interested parts is

TabBarController -> Profile View Controller --segue--> Navigation Controller -> Detail View Controller

The Detail View Controller contains a UICollectionView of images, each image having a tap gesture recognizer. What I'm trying to do is to tap on one of the images, pass the indexPath.row to the ProfileViewController and GO BACK programmatically.

When I go to the Detail View Controller there is a Back button already there so the idea is: if user presses on "Go Back" button, nothing happens. If user presses on image, the back button is triggered by itself and the image in the Profile changes.

So far I can tap on the images, pass the indexPath.row but I can't manage to go back to the previous View Controller.

I've tried navigationController?.popViewControllerAnimated(true)and navigationController?.popToRootViewControllerAnimated(true) but both of them don't work (I put them into the tap function for the gesture and nothing happens with regards to the navigationController, the tap function works fine)

When I print the following I get 1 navigationController.viewControllers.count

The only thing that I managed to get working is dismissViewControllerAnimated however that goes out of the TabBarController to another view that is before that.

Cœur
  • 37,241
  • 25
  • 195
  • 267
D. Mihai
  • 41
  • 5

2 Answers2

2

UPDATE

I figured it out, it had nothing to do with it programatically, the correct answer was to use navigationController?.popViewControllerAnimated(true) however my View Hierarchy was wrong

TabBarController -> Navigation Controller -> Profile View Controller --segue-> Detail View Controller

Now it works fine

D. Mihai
  • 41
  • 5
0

Please make property with strong attribute of NSIndexPath in both view controller Profile View Controller and Detail View Controller. Also, pass that object’s reference in prepareForSegue from view controller named Profile View Controller like code given below :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        Detail_View_Controller *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyIndexPath:self.object_indexPath];
    }
}

In Detail_View_Controller, when you tap on back button please update your selected index in the same property with creating one IBAction, which was passed from previous view controller.

I have taken reference from here.

Community
  • 1
  • 1
korat prashant
  • 1,523
  • 10
  • 24
  • @ korat-prashant I already have something that does this however I would like to use something native for going back without using segue. I've tried unwind segue but there was no action as well. – D. Mihai Jan 18 '16 at 14:21