Implementing delegation between views embedded in two separate container controllers

The relevant part of my storyboard is as follows: http://imgur.com/09livAb You can see that the custom "Container Controller" displays two Container Views, one of which is connected to the navigation controller through the built-in segment, and the other to the custom "Master View Controller" (which implements the table view controller) through the built-in segue. The navigation controller component also relates to the user-defined "location filter controller".

I need to implement delegation in such a way that when one of the UISteppers in the location filter controller is incr./decr., The table view in the main view controller knows to update the data that it displays accordingly.

I'm not used to working with protocols / delegates, but this unique situation in the conversation between the views hosted in segues really fools me! For the most part, I was successful, following the example here: Transferring data between controllers . In this case, however, I cannot directly link the created instances, as it indicates what needs to be done in step 6, “Transferring data back”.

I examined the use of a singleton object from which each of these views could get / set the necessary data, but the problem here is that the table view will not necessarily know when to update its contents, despite the presence of data with which it could / should be updated.

Here is the code snippet from ContainerController.m where I install the built-in functions:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { DataHold *data = [[DataHold alloc] init]; // <-- this actually is a singleton object if([segue.identifier isEqualToString:@"locationEmbedSegue"]) { } else if([segue.identifier isEqualToString:@"tableEmbedSegue"]) { [[segue destinationViewController] setDelegate:data.detailTableViewController]; // ^ This part actually sets up a delegate so that the table view (Master View Controller) // delegates to the detail view controller of the overarching split view controller // and tells it what to display when a row is pressed. } } 

Thanks for any help!

+6
source share
2 answers

I think you are on the right track by setting the table view delegate to your location filter controller.

I found that an easy way to work with the built-in view controller is to add the "placeholders" property for them and set this property when the "segue" is executed.

 // MyContainerController.h @property (strong, nonatomic) MyLocationFilterController *detailViewController; @property (strong, nonatomic) UITableViewController *masterViewController; // MyContainerController.m - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"locationEmbedSegue"]) { UINavigationViewController *dest = (UINavigationViewController *)segue.destinationViewController; self.detailViewController = dest.topViewController; } else if([segue.identifier isEqualToString:@"tableEmbedSegue"]) { self.masterViewController = (UITableViewController *)segue.destinationViewController; [self.masterViewController.tableView setDelegate:self.detailViewController]; } } 
+4
source

I recently came up with this question and found that there might be one problem with the answer above.

  • Move the setDelete: method. This ensures that there are no controllers.

Then the code becomes:

 // MyContainerController.h @property (strong, nonatomic) MyLocationFilterController *detailViewController; @property (strong, nonatomic) UITableViewController *masterViewController; // MyContainerController.m - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"locationEmbedSegue"]) { UINavigationViewController *dest = (UINavigationViewController *)segue.destinationViewController; self.detailViewController = dest.topViewController; } else if([segue.identifier isEqualToString:@"tableEmbedSegue"]) { self.masterViewController = (UITableViewController *)segue.destinationViewController; } [self.masterViewController.tableView setDelegate:self.detailViewController]; } 
0
source

Source: https://habr.com/ru/post/946621/


All Articles