Accessing instance methods of parent view from child view?

Discussion in 'iPhone/iPad Programming' started by neil.b, Dec 4, 2008.

  1. macrumors member

    neil.b

    Joined:
    Nov 20, 2008
    #1
    I've got an instance method in my view controller that I want to call from one of the view controller's sub views.

    Is there a way to do this? In Actionscript (for example) you can use parent.function but for the life of me I can't figure it out in ObjC.

    Thanks
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    You could extern the view controller you want to access and then call that from the sub view, i.e.:

    Code:
    extern YourViewController *yvc;
    in YourViewController.h;

    Code:
    YourViewController *yvc;
    in YourViewController.m (before you declare the @implementation); and

    Code:
    [yvc yourInstanceMethod];
    from the sub view. (You'll need to #import YourViewController.h into YourSubView.m.)
     
  3. thread starter macrumors member

    neil.b

    Joined:
    Nov 20, 2008
    #3
    I'll give that a try, thanks :)
     
  4. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    I would stick a pointer to the superview inside the subview object, and have that pointer populated by the superview on init or load. That way the superview is more in control of its own use.

    .
     
  5. thread starter macrumors member

    neil.b

    Joined:
    Nov 20, 2008
    #5
    Nice. I like that one.

    :)
     

Share This Page