Showing ads on every view automatically in iPhone
I'm implementing Mobclix in my app, and when following the instructions, it tells us to properly release and dealloc the adview. It also tells us that when the view appears, you should call refreshAds and when it disappears you should pause refresh ads:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
@implementation RootViewController
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
}
- (void)dealloc {
[self.adView cancelAd];
self.adView.delegate = nil;
self.adView = nil;
[super dealloc];
}
@end
My question is that I have a lot of view controllers, so it would be inconvenient to implement these things on each view. Is there anyway I can call these methods in one place and have it 开发者_开发知识库work for all views?
What you can do is subclass UIViewController
and have all your viewController
s to inherit these things.
So basically you could have AdsViewController.h
wich will look like
#import <Foundation/Foundation.h?
@interface AdsViewController: UIViewController {
}
@property (nonatomic, retain) <#AdsViewType#> *adsView;
@end
and your AdsViewController.m
will have to look pretty much like this
#import "AdsViewController.h"
@implementation AdsViewController
@synthesize adsView = _adsView;
- (id)init {
self = [super init];
if (self) {
_adsView = [[<#AdsViewType#> alloc] init];
_adsView.delegate = self;
}
return self;
}
- (void)viewDidUnload {
[self.adView cancelAd];
self.adView = nil;
[super viewDidUnload];
}
- (void)dealloc {
[self.adView cancelAd];
self.adsView = nil;
[super dealloc];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.adView resumeAdAutoRefresh];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.adView pauseAdAutoRefresh];
}
@end
After this have all your viewController
s to subclass this instead of UIViewController
. Hope this helps.
精彩评论