简体   繁体   English

呈现和嵌入式视图控制器之间的交互

[英]interactions between presented and embedded view controllers

  • VC1 embeds VC2 in a container view. VC1将VC2嵌入到容器视图中。 VC2 is a table VC. VC2是表VC。

  • Clicking a cell in VC2 pushes VC3. 单击VC2中的单元格将推动VC3。

  • VC3 embeds VC4 in a container view. VC3将VC4嵌入到容器视图中。

视觉的 How do I get a reference to VC1 from within VC4.m ? 如何从VC4.m中获得对VC1引用

I tried self.parentViewController.presentingViewController.parentViewController and self.parentViewController.presentingViewController but they didn't seem to work. 我尝试了self.parentViewController.presentingViewController.parentViewControllerself.parentViewController.presentingViewController但它们似乎没有用。

But then I decided to see if it would work when I used the delegate to store a reference to VC1, and still I was getting null for all of the public properties on VC1. 但是后来,我决定看看当我使用委托存储对VC1的引用时是否可行,但对于VC1上的所有公共属性,我仍然为空。 Would VC1 not be in memory in this scenario? 在这种情况下,VC1不会在内存中吗? If not, why not? 如果没有,为什么不呢? If so, why else would its (strong) properties be null? 如果是这样,为什么其(strong)属性为null?

edit: I've also just discovered through NSLogs that viewDidLoad in VC4 executes before didSelectRowAtIndexPath in VC2 finishes executing and setting the delegate reference, which may explain why that approach isn't working. 编辑:我也刚刚通过NSLogs发现,VC4中的viewDidLoad在VC2中的didSelectRowAtIndexPath完成执行和设置委托引用之前已执行,这可能解释了为什么这种方法行不通。 How do I ensure the next VC is pushed only at the completion of all other lines in didSelectRowAtIndexPath ? 如何确保仅在didSelectRowAtIndexPath中所有其他行的完成时才推送下一个VC? And regardless, there are other public properties on VC1 that are already set (not null) prior to a row being selected (I've just verified this with an NSLog in didSelectRow in VC2), and they are turning up null when I attempt to access them through VC4 through any approach. 而且无论如何,在选择行之前,VC1上已经设置了其他公共属性(不为null)(我刚刚在VC2的didSelectRow中使用NSLog进行了验证),并且当我尝试执行以下操作时它们会变为null通过任何方法通过VC4访问它们。

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */

// set reference to selected convo. it is stored in ConversationsParentVC public property
self.conversationsParentVC.selectedConvo = self.conversationsParentVC.conversationsArray[indexPath.row];

// temp
AppDelegate *delegate = getAppDelegate;
delegate.activeVC = self.conversationsParentVC;
ConversationsParentVC *convosParentVC = (ConversationsParentVC*)delegate.activeVC;
NSLog(@"boop %@", delegate.activeVC);
NSLog(@"beep %i", convosParentVC.conversationsArray.count);
}

堆栈跟踪

When you set up a push segue from a table view cell, the segue fires before the tableView:didSelectRowAtIndexPath: method is called. 当您从表视图单元格设置推送segue时,该segue在调用tableView:didSelectRowAtIndexPath:方法之前触发。 You need to pass your information on to the destination view controller in the source view controller's prepareForSegue:sender: method. 您需要通过源视图控制器的prepareForSegue:sender:方法将信息传递到目标视图控制器。

Also, you might find this answer helpful. 另外,您可能会发现此答案很有帮助。

You may also find it useful to know that the sender argument in prepareForSegue:sender: is the table view cell, and the table view's indexPathForSelectedRow has already been set to the cell's index path. 您可能还会发现,知道prepareForSegue:sender:中的sender参数是表视图单元格,并且表视图的indexPathForSelectedRow已设置为单元格的索引路径,这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM