0

https://i.stack.imgur.com/h3hnz.jpg

In the picture provided above, shows three view controllers.

Let's call the view controllers A, B, and C in the order they're displayed.



View Controller A passes parsed jSON data by clicking on the table cell. This fills up the "total sales, discounts, etc." strings/labels inside of the View Controller B using a prepareForSegue method.

This data is based on a start time/end time and the default parameter when performing the segue gives information from 8 AM to 10 PM of the current day.

The parsing string looks like this

NSDate *currentDate = [NSDate date];
            NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
            [dateformatter setDateFormat:@"YYYY-MM-dd"];
            NSString *theDate = [dateformatter stringFromDate: currentDate];

NSString *salesStr = @"http://";
salesStr = [salesStr stringByAppendingString:host];
salesStr = [salesStr stringByAppendingString:@":8080/sales.php?password="];
salesStr = [salesStr stringByAppendingString:pass];
salesStr = [salesStr stringByAppendingString:@"&db="];
salesStr = [salesStr stringByAppendingString:db];
salesStr = [salesStr stringByAppendingString:@"&sdate="];
salesStr = [salesStr stringByAppendingString:theDate];
salesStr = [salesStr stringByAppendingString:@"%2008:00:00&edate="];
salesStr = [salesStr stringByAppendingString:theDate];
salesStr = [salesStr stringByAppendingString:@"%2022:00:00"];

Inside of the View Controller B, you can notice a button labeled "Start Time". This button initiates a push segue to View Controller C. In this new view controller, you are able to select a date and time and it is displayed in the UILabel above the date picker. The button below the date picker is an IBAction and uses

[self.navigationController popViewControllerAnimated:YES];



My question is: How am I able to select a date/time in View Controller C, press a button, and send that information located in the UILabel to View Controller B so it can be used to update the parsed information?

One way I could think of doing this is to have the button segue to View Controller B, and RE-PARSE the information based on the date/time selection, but that would just cause too much 'navigation controller stacking' and just doesn't seem efficient to me.

Any suggestions are appreciated.

Machina
  • 252
  • 2
  • 13

2 Answers2

2

Hacker's approach would work.

Apple's documentation suggests a slightly different technique. Define a simple protocol with a method that view controller C can use to communicate with view controller B.

Then give view controller C a delegate property that conforms to that protocol.

In your prepareForSegue method, set yourself as view controller C's delegate.

Then, in view controller C, when the user changes the date and clicks the button, invoke a delegate method to notify view controller B that the user changed the date value before popping yourself.

I would suggest adding a cancel button as well, so the user can discard their changes. In that case, you just would skip calling the delegate method before popping.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Question, sir. I ended up doing what you said and learned a great deal about protocol! Should I be implementing all my parsings and data retrieval in View Controller B? Right now, all the data is being retrieved through View Controller A and then it sends it to View Controller B. – Machina Jun 05 '14 at 03:00
  • It would be better to create a separate parsing object and use that, instead of doing it in *either* VC. – Duncan C Jun 05 '14 at 11:33
  • Oh so you mean like using the protocol NSObject method? Paste my data retrieval in that and then call the protocol when needed? – Machina Jun 05 '14 at 12:24
  • I don't understand what you mean. You should implement a new custom object that has methods for parsing data and returning results to the caller (either through delegate methods or with blocks.) The new custom object will probably be a subclass of NSObject, and might be a singleton. – Duncan C Jun 05 '14 at 13:06
1

in the interface of your view controller C add a property

@property (nonatomic, strong) NSDate *selectedDate;

before pushing C from B do

[cViewController addObserver:self forKeyPath:@"selectedDate" options:0 context:NULL];

and implement

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    //do some stuff
}

in view controller B.

when selecting a new date do (in view controller V)

self.date = myPicker.date;

so view controller B gets notified when the new date is set

don't forget to remove the observer in implementation of B (e.g. viewWillAppear)...

[cViewController removeObserver: self....];
RTasche
  • 2,614
  • 1
  • 15
  • 19
  • When you say do some stuff, can you give an example? – Machina Jun 04 '14 at 23:31
  • an example? perform those actions / calculations that are required after selecting the date. I suppose there's a reason why you let the user select a date... – RTasche Jun 06 '14 at 11:55