在iPhone中自动在每个视图上显示广告

时间:2011-08-07 18:39:37

标签: iphone objective-c

我正在我的应用程序中实现Mobclix,并且在遵循说明时,它告诉我们正确发布和取消分享广告。它还告诉我们,当视图出现时,您应该调用refreshAds,当它消失时,您应该暂停刷新广告:

 - (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

我的问题是我有很多视图控制器,所以在每个视图上实现这些东西都不方便。无论如何,我可以在一个地方调用这些方法并让它适用于所有视图吗?

1 个答案:

答案 0 :(得分:1)

你可以做的是子类UIViewController并让你所有viewController继承这些东西。

所以基本上你可能会AdsViewController.h看起来像

#import <Foundation/Foundation.h?

@interface AdsViewController: UIViewController {
}

@property (nonatomic, retain) <#AdsViewType#> *adsView;

@end

并且您的AdsViewController.m必须看起来非常像

#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

在此之后,让所有viewController为子类而不是UIViewController。希望这会有所帮助。