0

In my project I have 3 controllers;

  • NavigationController
  • ServiceTableViewController
  • DateTableViewController

The ServiceTableViewController is the initial view controller. It has several rows which prompt the user to enter in data, which will be emailed to a particular email address. One of the rows, when tapped, sends the user to the DateTableViewController which prompts the user to select a date from the UIDatePicker.

The issue I am facing is getting data back from DateTableViewController in order to display a label on the ServiceTableViewController to show the date the user selects in the DateTableViewController. I know how to get information from one view controller to another, but to go in reverse, so to speak, is not something I know how to do. Any help is appreciated.

Moshe
  • 57,511
  • 78
  • 272
  • 425
Green Developer
  • 187
  • 4
  • 18

2 Answers2

0

Research delegate pattern (here) (a heavily used pattern within Apple frameworks). You want to define a delegate protocol which allows to a date to be passed to the delegate.

You could implement the pattern as an @protocol with a single method and a property on the DateTableViewController. The ServiceTableViewController sets itself as the delegate before pushing the DateTableViewController.

Or, you could implement using a block. Again, the ServiceTableViewController sets the block before pushing the DateTableViewController.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

Take a look at this: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html

There are couple of ways to pass data back and forth between view controllers.

but honestly delegates are really all you need really and it sounds like in your current case. see this -> (Passing Data between View Controllers)


Having said that, if you use delegates, here is how --- setup a protocol in DateTableViewController.h at the top like so:

@protocol DateTableViewControllerDelegate <NSObject>
- (void)userSelectedThisDate:(NSDate *)d;
end

put this with the other properties

@property (nonatomic, weak) id <DateTableViewControllerDelegate> delegate;

and in DateTableViewController.m with the date to send back

[self.delegate userSelectedThisDate:withTheDateToSendBack];

in and ServiceTableViewController.h add

#import "DateTableViewController.h"
@interface ServiceTableViewController : UIViewController <DateTableViewControllerDelegate>

and since you are UINavigationController, somewhere in ServiceTableViewController.m add this when you are about to push to the DateTableViewController

DateTableViewController *vc = [[DateTableViewController alloc] init];
self.delegate = self;
[self.navigationController pushViewController:vc animated:YES];

and finally put the delegate method in ServiceTableViewController.m

- (void)userSelectedThisDate:(NSDate *)d {
   NSLog(@"%@", d); // this should show the returned date 
}
Community
  • 1
  • 1
july
  • 332
  • 1
  • 6