views:

34

answers:

1

I've just implemented iAds in an app with several distinct UIViewControllers. I have the delegate methods in each one for - (void)bannerViewDidLoadAd:(ADBannerView *)banner and - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error that show and hide the banner, along with a setup method that creates a banner during viewDidLoad.

I did this by getting everything working in the first UIViewController and then copying the code to each of the other controllers, along with the ivar declarations, properties, etc...

This can't be right. Every time I change one, I have to change them all. My question (finally!) is: Is there a way to write these methods once so that all classes have access to them?

Thanks!

A: 

i recommend creating a c function if subclassing does not make sense.

if this function accompanies other related functions, place it in a library with its relatives. if the list becomes long, a class may have been a better choice.

if this function is standalone and specific to one app/project, then just create a file which declares all of these odd bits (if they must be reused across multiple translations). if the file of bits is really large, there's likely a few design issues.

some people may recommend an objc category instance method. i generally avoid categories because the chance for error is unnecessarily high. C and C++ functions/types, are linked and easily stripped if they are not referenced. with a category, you don't link, and it and the symbols used in the method must not be stripped.

you could include this as an instance method for your singleton, if the two are conceptually (or physically) bound. otherwise, i recommend keeping them separate.

Justin
Could you elaborate? Where would the C function reside? I have a singleton object that takes care of saving state and such - could the C function (I would expect at least two of them) be there? Would I pass in the instance of `adBanner` for it to process?
Steve
@Steve response updated
Justin
Thanks, Justin. I never did any C/C++ programming before starting to use ObjC, so I constantly feel like I'm starting from scratch. I've never heard of a Category. I'm going to try and put the methods in their own file and import that file in each view controller - stay tuned!
Steve