iOS Sharing methods between views

nathanlloyd

macrumors member
Original poster
Dec 7, 2009
70
0
UK
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.
 

ppilone

macrumors 6502
Jan 20, 2008
361
0
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.
 

nathanlloyd

macrumors member
Original poster
Dec 7, 2009
70
0
UK
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.
 
Last edited:

nathanlloyd

macrumors member
Original poster
Dec 7, 2009
70
0
UK
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?
 

ppilone

macrumors 6502
Jan 20, 2008
361
0
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.