0

I have created a View Controller with a UITableView into it. When I tap a cell, it leads the user to another view with a Table View of options with the method :

[self.navigationController pushViewController:optionsView animated:YES];

On this view, I want that tapping a cell makes the user go to the previous view. So I use :

[self.navigationController popViewControllerAnimated:YES];

Which works perfectly. But I just don't know how to keep the index of the cell chosen. Any advice ?

Thanks a lot

Rob
  • 15,732
  • 22
  • 69
  • 107

3 Answers3

1

You can make the first view controller a delegate of the second one by defining a delegate property in the optionsView class and setting it to self before pushing the controller. Then, before popping the controller, you can send whatever information you need back to the first controller through the delegate reference.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Thank you. Could you give me a little code example ? I don't really know how to use delegate... – Rob May 26 '12 at 13:30
  • From the other answer given, I see that there is something that isn't clear: which index do you want to remember, from the first table or the second table...and which controller do you want it available in? – Phillip Mills May 26 '12 at 13:37
  • In fact, I've a 3-cells table in the first view. Tapping one of the cells leads to a view with another table. What I want here is when I tap a cell of this second view, it makes the app coming back to the previous view (this works), and keep the row index selected to use it in the first view. – Rob May 26 '12 at 13:41
1

So in the parent view controller, declare a class member NSIndexPath *_selectedIndexPath;

In didSelectRowAtIndexPath method, before pushing the child view controller, do as follows:

if( _selectedIndexPath )
   [_selectedIndexPath release];

_selectedIndexPath = [indexPath retain];

So even after you come back, you can get the values from the _selectedIndexPath.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

You need to use delegate method. It is commonly used pattern in Objective-C. I have an example answer from this post. Let me know if you are not clear afterward.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
  • I was almost sure I would have to use it. But as I never used delegates, first I have to learn a little bit. Thank you very much for having replied. :) – Rob May 26 '12 at 15:37