在嵌入两个独立容器控制器的视图之间实现委派 [英] Implementing delegation between views embedded in two separate container controllers
问题描述
我的故事板的相关部分如下所示:
您可以看到自定义的容器控制器视图包含两个容器视图,一个通过嵌入式视图链接到导航控制器,另一个链接到自定义主视图控制器(实现表视图控制器)。导航控制器组件还与定制的位置过滤器控制器有关系。
The relevant part of my storyboard appears as follows: You can see the custom "Container Controller" view houses two Container Views, one which links to a Navigation Controller via embedded segue, and another that links to a custom "Master View Controller" (which implements a Table View Controller) via embedded segue. The Navigation Controller component further has a relationship with a custom "Location Filter Controller."
我需要实现委派,使得当位置过滤器控制器中的一个UISteppers incr./decr。主视图控制器中的表视图知道更新相应显示的数据。
I need to implement delegation such that when one of the UISteppers in the Location Filter Controller is incr./decr., the table view in the Master View Controller knows to update the data it displays accordingly.
我不习惯使用协议/代表,但是这种独特的交谈意见让人感到窒息!在大多数情况下,我已经成功地遵循以下示例:在视图控制器之间传递数据 。然而,在这种情况下,我无法直接链接实例化的视图,就像他指出的那样,在传回数据后退步骤6中。
I am not unaccustomed to working with protocols/delegates, but this unique situation of talking between views housed in segues is really tricking me! For the most part I have had success following the example here: Passing Data between View Controllers. In this case however, I am not able to directly link instantiated views as he indicates to do in 'Passing Data Back' step 6.
我曾经考虑过使用单例对象每个这些视图都可以从中获取/设置必要的数据,但这里的问题是表视图不一定知道何时更新其内容,尽管有可能/应该更新的数据。
I had considered using a singleton object from which each of these views could get/set the necessary data, but the issue here is that the table view would not necessarily know when to update its contents, despite having data with which it could/should update.
这是一个来自ContainerController.m的代码片段,其中我设置了嵌入的段落:
Here is a code snippet from ContainerController.m where I setup the embedded segues to function:
- (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.
}
}
感谢任何帮助!
推荐答案
我认为您是正确的轨道,将表格视图委托给您的位置过滤器控制器。
I think you are on the right track setting the table view delegate to your Location Filter Controller.
我发现使用嵌入式视图控制器的简单方法是为它们添加占位符属性,并在执行时设置这些属性。
I found that a simple way to work with embeded view controller is to add "placeholders" property for them, and set these property when the segue is "performed".
// 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];
}
}
这篇关于在嵌入两个独立容器控制器的视图之间实现委派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!