Rob
Rob

Reputation: 16002

Navigate between UITableViews and views and keep data?

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

Upvotes: 0

Views: 83

Answers (3)

user523234
user523234

Reputation: 14834

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.

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

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.

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

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.

Upvotes: 1

Related Questions