Sharing methods between views

Discussion in 'iPhone/iPad Programming' started by nathanlloyd, Mar 12, 2012.

  1. macrumors member

    Joined:
    Dec 7, 2009
    Location:
    UK
    #1
    Hi guys, I've searched through the forums for an answer to this but can't find an answer that hits the nail on the head.

    I started an app and added methods to my main UITableViewController but I'm now discovering that other UITableViewControllers could borrow the methods I added for the first.

    What's the best way to share code amongst these? I'm guessing I need another file in my project, but I'm not sure what type of file this would be.

    Any help would be appreciated.

    Thanks.
     
  2. macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    Without knowing the exact code you intend on sharing, the simplest solution would be to create a common UITableViewController subclass that contains the methods you wish to share. You should then inherit from that controller throughout your application where appropriate.
     
  3. macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #3
    You could also see if categories will work for your situation.
     
  4. nathanlloyd, Mar 12, 2012
    Last edited: Mar 12, 2012

    thread starter macrumors member

    Joined:
    Dec 7, 2009
    Location:
    UK
    #4
    OK, so is that perfectly acceptable even though no UITableViewController will ever be attached directly to it?

    Sorry just to explain a little more, the shared code is a method that extracts data from JSON and dumps it in a NSMutableArray that the Views then use.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    This sounds like code that is better suited to being implemented in the Model part of your MVC design.
     
  6. thread starter macrumors member

    Joined:
    Dec 7, 2009
    Location:
    UK
    #6
    OK, I'll do some more research into MVC design.

    Thanks for your help.
     
  7. thread starter macrumors member

    Joined:
    Dec 7, 2009
    Location:
    UK
    #7
    MVC - this is precisely what I need to implement so I can share code across my views.

    My question is: When I'm adding my model files, what kind of files are they? I know that might not make sense, but for example when I'm adding class files, I make them of type NSObject. Do these files that hold my methods need to be of a particular type?
     
  8. macrumors 6502

    Joined:
    Jan 20, 2008
    #8
    In the simplest case your model is just a subclass of NSObject. There is no rule that dictates this (obviously wouldn't be a subclass of something like UIView) and it can depend on the framework you're using (e.g. CoreData models are subclasses of NSManagedObject).

    In your case it sounds like a simple subclass of NSObject is all you need.
     
  9. thread starter macrumors member

    Joined:
    Dec 7, 2009
    Location:
    UK
    #9
    Cheers ppilone,

    I'll try this tonight.

    Thanks for everyone's input.
     

Share This Page