[英]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.
您可以看到自定义“容器控制器”视图包含两个容器视图,一个通过嵌入式segue链接到导航控制器,另一个通过嵌入式segue链接到自定义“主视图控制器”(实现表视图控制器)。 The Navigation Controller component further has a relationship with a custom "Location Filter Controller."
导航控制器组件还具有与定制“位置过滤器控制器”的关系。
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. 我需要实现委托,以便当位置过滤器控制器中的一个UISteppers为incr./decr。时,主视图控制器中的表视图知道更新它相应显示的数据。
I am not unaccustomed to working with protocols/delegates, but this unique situation of talking between views housed in segues is really tricking me! 我并不习惯使用协议/代表,但是这种在segues中观看的独特情况实际上是在欺骗我! 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.
但是,在这种情况下,我无法直接链接实例化视图,因为他指示在“传递数据”步骤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. 我曾考虑使用一个单独的对象,其中每个视图都可以获取/设置必要的数据,但问题是表视图不一定知道何时更新其内容,尽管它具有可以/应该更新的数据。
Here is a code snippet from ContainerController.m where I setup the embedded segues to function: 这是一个来自ContainerController.m的代码片段,我将嵌入的segues设置为函数:
- (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! 谢谢你的帮助!
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". 我发现使用嵌入式视图控制器的一种简单方法是为它们添加“占位符”属性,并在“执行”segue时设置这些属性。
// 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];
}
}
I came to this question recently and found there may be one problem with the answer above. 我最近提到了这个问题,发现上面的答案可能有一个问题。
Then 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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.