View Full Version : How do you send data to the previous view?

Jan 11, 2011, 12:26 PM
So i've got a table view with several sections and i want to make a filter so only a few sections are displayed. I've already created a view so the sections can be chosen. There I've got a table where you can select the wanted sections for the first view. and now i don't know how to use a protocol or delegate to send the data back to my previous view.
so here is the code of the fist view:

@implementation TableViewController

- (NSArray *)sections {

if (!sections) {

sections = //creating sections
return sections;


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];

UIBarButtonItem *settingsButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(filterview)];
self.navigationItem.leftBarButtonItem = settingsButtonItem;
[settingsButtonItem release];



#pragma mark -
#pragma mark Table view delegate

- (void)filterview

SettingsTableViewController *settingsTableViewController = [[SettingsTableViewController alloc] init];
[[self navigationController] pushViewController:settingsTableViewController animated:YES];
[sections release];
[settingsTableViewController release];


and here is the code of the FilterViewController which will be displayed:

@implementation SettingsTableViewController
- (NSMutableArray *)selections
//here are all selected items saved
if (!selections) {
selections = [[NSMutableArray alloc] init];
return selections;

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Settings";
self.navigationItem.hidesBackButton = YES;

UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:@selector(/*some method to send data back*/)];
self.navigationItem.rightBarButtonItem = saveItem;
[saveItem release];

and now i want to send this data (selections) back, how do I do this? HELP!

Jan 13, 2011, 06:54 AM
How bout making a reference to that view.
And do like in FirstView
[secondView arrayName]
or secondView.arrayName? :)

Jan 13, 2011, 07:19 AM
If all your doing is filtering sections there is no need for a new view. You can redraw your original UITableView and then filter out the sections.

Back to what your trying to do. If you know you want to use a delegate \ protocol paradigm however you're unsure how to implement such a thing I would suggest looking in the apple developer center a quick search of protocol returned this http://developer.apple.com/library/ios/#samplecode/MultipleDetailViews/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40009775-Intro-DontLinkElementID_2, note this will only show you how to implement a protocol, implementing a delegate is even easier.