简体   繁体   中英

interactions between presented and embedded view controllers

  • VC1 embeds VC2 in a container view. VC2 is a table VC.

  • Clicking a cell in VC2 pushes VC3.

  • VC3 embeds VC4 in a container view.

视觉的 How do I get a reference to VC1 from within VC4.m ?

I tried self.parentViewController.presentingViewController.parentViewController and self.parentViewController.presentingViewController but they didn't seem to work.

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. Would VC1 not be in memory in this scenario? If not, why not? If so, why else would its (strong) properties be 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. How do I ensure the next VC is pushed only at the completion of all other lines in didSelectRowAtIndexPath ? 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.

(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. You need to pass your information on to the destination view controller in the source view controller's prepareForSegue:sender: method.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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