How to pass parameters to a popover view controller inside a navigation controller
Asked Answered
B

2

4

I have an iPad app with splitview and a right bar button in the navigation bar of the detailviewcontroller.

This button calls a popover constituted of a navigation controller and two table views. as shown in the following picture

enter image description here

As the storyboard is small on the picture I add some explanation. The White controller is the detailViewController (right hand side of the splitviewcontroller), which is inside a navigtion controller. The three views on the right side of the white view are from left to right : navigationController, firstTableView, secondTableView.

My problem is that I need to pass parameters to the first table view to configure it correctly.

I use to do it like that when view controller were sharing the same navigation controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"popoverButtonSegue"]){
        MyPopoverFirstTableViewController *popoverFirstTVC = [[MyPopoverFirstTableViewController alloc] init];

        popoverFirstTVC = segue.destinationViewController;
        popoverFirstTVC.property1 = aProperty1;
        popoverFirstTVC.property2 = aProperty2;
    }
}

My Problem is that I get an error doing this because the destination controller is not my popoverFirstTableViewController but the NavigationController and the navigation controller of course does not haver "property1" and "property2".

Perhaps it's a basic question but I am stuck here.

How do I do to configure my tableview by assigning some of its properties I have defined when this table is inside a navigation controller ?

Benevolence answered 27/11, 2012 at 19:25 Comment(0)
B
4

All the credit belongs to @Michael Kernahan, but as long as he don't post it as an answer I'll write it as a follow up for people looking for the same answer.

In my case the problem was that I am assigning the destination controller which is the navigation controller

popoverFirstTVC = segue.destinationViewController; 

what I should do is to access the topViewController of that navigation controller.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"popoverButtonSegue"]){
        MyPopoverFirstTableViewController *popoverFirstTVC = (MyPopoverFirstTableViewController *)((UINavigationController *) segue.destinationViewController).topViewController;

        popoverFirstTVC.property1 = aProperty1;
        popoverFirstTVC.property2 = aProperty2;
}
Benevolence answered 29/11, 2012 at 16:41 Comment(1)
Great question, great answer. +1 to both!Innervate
L
0

I'm not entirely sure I follow, because the storyboard is a bit small for me to read. But you seem in your code to have created a sparkling new popoverFirstTVC and then thrown it away by the assignment

popoverFirstTVC = segue.destinationViewController;

If you comment that line out, then at then end write

segue.destinationViewController.itsPopover = popoverFirstTVC;

then you can pass off your fully-constructed and initialised popoverFirstTVC and have code in the second VC pick it up and run with it. Of course, you need

@property (strong, readwrite) MyPopoverFirstTableViewController * itsPopover;

in the header for your second VC.

Lovelorn answered 27/11, 2012 at 20:28 Comment(5)
I've made an Edit trying to describe the storyboard. What I show in code is the usual way of passing parameters to a viewcontroller in case of a push. I get the same error in what you gave me because the segue still points to the navigation controller where the firstTVC is the one I want to configure.Benevolence
Sorry, I misunderstood entirely. (Perhaps I still do!) So segue.DestinationViewController is a UINavigationController? Does that do a segue to a MyPopoverFirstTableViewController which is the next box along on the diagram? In which case it'll not instantiate the destination until the segue, so you'll need some other way to transfer the data. Or is the MyPopoverTableViewController one of the controllers managed by the UIViewController? So you could use segue.destinationViewController.viewcontrollers[n].property1 = ...Lovelorn
Almost there: .topViewController property on the UINavigationController will probably get you what you want.Intelligible
@MichaelKernahan that's it. Write it as an answer and I'll validate itBenevolence
By the way @Lovelorn thank you to have taken the time to answer. The answer of Michael Kernahan was what I was looking forBenevolence

© 2022 - 2024 — McMap. All rights reserved.