How to Call Parent View from Child View?

Discussion in 'iOS Programming' started by belsokar, Aug 7, 2008.

  1. belsokar macrumors member

    Jul 21, 2005
    So, one issue I have run into often is trying to get data from a parent view in a child view.

    For example, Consider I have a standard navigation drill down application, with RootViewController, View2Controller, and View3Controller. (i.e. Root Navigates to View2 which navigates to View3). In this example, if I am in View3, and I want to access "shared" data that was created in view2, then I need to either pass the value in when I display View3, or I have to create a global object in my AppDelegate class and then access it that way using the following code:

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    This is ok in some instances, but it would be much better if at times I can access properties of a parent view directly from the child view. I have been tinkering with this for a while now, and have yet to find an easy approach. Any ideas?
  2. caldwelljason macrumors member

    Jul 9, 2008

    this is not the cleanest of solutions, but since no one is responding with the "right" way... ;) ...

    You could add a property to your child view which is of type "pointer to parentView." Then when you initialize the child view, set its parent pointer to "self." That way, from the child, you could get to the parent.

    Another possibility is to use the navigation controller's stack to access a previous view. I believe that in addition to the stack-like "push-pop" interface, you can treat it like a read-only array, accessing views in the stack at specified positions.

    Anyway, just a couple of ideas...
  3. seventoes macrumors newbie

    Feb 5, 2008
    I do it the ugly way, passing a pointer to the view to the child. You can also get a read-only copy by using
    [someRandomView superview];
  4. Sbrocket macrumors 65816


    Jun 3, 2007
    That's not to say that the view is somehow "read-only" (which is what it sounds like you're saying). That's the easiest way to access the superview, and the superview property itself is read-only so that you can't redefine a view's superview using it.
  5. kainjow Moderator emeritus


    Jun 15, 2000
    You probably don't want to go down this path. First of all, your views shouldn't be managing where they get their data from. That is the job of the view controller. The view controller tells a view what its data is, and the view controller is responsible for obtaining this data. So if you are using a navigation controller, if you push a new view controller that needs data from an existing view controller, set this data first, then push the view controller. Or use a model with a shared data object that both view controllers can access.
  6. Sbrocket macrumors 65816


    Jun 3, 2007
    This is a model I've personally favored for my own coding. Just make sure that you aren't instantiating model copies of this shared data object for each time you need to use it or you'll end up with lots of memory used that you don't need to use, and this is much more important for a mobile platform such as the iPhone OS. Use a singleton model.

Share This Page