Richard McKenna
Richard McKenna

Reputation: 313

segue loading viewcontroller but not displaying it

I have a problem displaying a View Controller when calling a segue from code in an iPad app.

I have the segue setup in IB by Ctrl dragging from one view controller to the other, with

Identifier: viewDocumentSegue, Style: Modal, Presentation: Default, Transition: Default

I'm then calling the segue from the first view controller using

[self performSegueWithIdentifier:@"viewDocumentSegue" sender:nil];

and passing a variable to the second view controller using

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *destination = segue.destinationViewController;

    if ([destination respondsToSelector:@selector(setFileURL:)]) {
        [destination setValue:fileName forKey:@"fileURL"];
    }
}

The second view controller does get loaded, as the NSLogs I have in viewDidLoad viewWillAppear and viewDidAppear show in the console, but nothing gets displayed.

I'm thinking the second view controller's view is somewhere in the view hierachy. But how do I force it to the top?

Edit

Here is a screenshot of the interface (some thing needed to be blacked out). I've added some notes explaining the structure. Hopefully it will help.

(I can't upload images yet so here's a link) http://www.linfoots.com/Screenshot.jpg

Any suggestions?

Thanks,

Richard

Upvotes: 0

Views: 2263

Answers (1)

matt
matt

Reputation: 535325

You're not providing the additional info I've asked for, so it's impossible to answer your question in detail. What I suggest you do is make a completely new project with just two view controllers. Connect them in the storyboard in the same way. You will see that what you're trying to do should in fact work just fine. Once you've convinced yourself of that, you can go back and see what the difference is in the project you're having trouble with.

EDIT:

Okay, thanks for the screen shot. It looks like you're trying to use a storyboard in connection with a parent view controller, exactly as I guessed in my second comment. That's not easy because a storyboard knows nothing of your parent view controller's children. You will be much happier using multiple storyboards, or no storyboard at all.

Now we come to the problem of using a parent view controller. You didn't ask about this, but it could be at the heart of your issue. It is crucial that you manage child view controllers properly. See the discussion in my book:

http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

You must do this elaborate dance every time, or things won't go right:

When a view controller is to become your parent view controller’s child:

  • You call addChildViewController: on your parent view controller. The child is automatically added to the parent’s childViewControllers array, which retains it.
  • You get the child view controller’s view into your interface, if that’s what adding a child view controller means.
  • You send didMoveToParentViewController: to the child with the parent view controller as its argument.

When a view controller is to cease being your parent view controller’s child:

  • You send willMoveToParentViewController: to the child with a nil argument.
  • You remove the child view controller’s view from your interface, if that’s what removing a child view controller means.
  • You send removeFromParentViewController to the child. The child is automatically removed from the parent’s childViewControllers array, which releases it.

Finally, when you do a modal transition, definesPresentationContext in your parent chain becomes crucial so that the modal view knows what view to replace in the interface. This downloadable example from my book demonstrates the necessary distinctions:

https://github.com/mattneub/Programming-iOS-4-Book-Examples/tree/master/convertedToIOS5/p476containerController2

Upvotes: 2

Related Questions