2012-05-08 5 views
6

मेरे पास प्रत्येक में 5 व्यू कंट्रोलर हैं जो मुझे आईएडी प्रदर्शित करना है, ताकि मुझे प्रत्येक व्यू कंट्रोलर में iAd कोड लागू करना पड़े। इसके बजाय यदि मैं ऐपडिलेगेट में सामान्य कोड का सेट बनाता हूं तो इसका मतलब है कि मैं उस कोड को कॉल कर सकता हूं जहां मुझे प्रदर्शित होने के लिए iAd चाहिए।आईएडी के लिए वैश्विक संदर्भ कैसे बनाएं और एकाधिक व्यू कंट्रोलर में कार्यान्वित करें

अगर किसी ने इसे लागू किया है तो आईएडी अवधारणा का मतलब है कि मुझे इस मुद्दे से बाहर निकलने में मदद करें। अग्रिम में धन्यवाद।

उत्तर

5

बस एपीपी प्रतिनिधि में आईएडी में एक पॉइंटर बनाएं।

ज:

@property (strong, nonatomic) ADBannerView *UIiAD; 

मीटर:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    UIiAD = [[ADBannerView alloc] init]; 
    return YES; 
} 
फिर अपने ViewControllers में

ऐसा करते हैं:

ज:

@property (strong, nonatomic) ADBannerView *UIiAD; 

मीटर:

- (AppDelegate *) appdelegate { 
    return (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
} 

- (void) viewWillAppear:(BOOL)animated { 
    UIiAD = [[self appdelegate] UIiAD]; 
    UIiAD.delegate=self; 
    // CODE to correct the layout 
} 

- (void) viewWillDisappear:(BOOL)animated{ 
    UIiAD.delegate=nil; 
    UIiAD=nil; 
    [UIiAD removeFromSuperview]; 
} 

लेआउट को फिर से डिजाइन करने के लिए उचित कोड वाले सभी दृश्य नियंत्रकों के लिए ऐसा करें!

1

हाय यह एक अच्छा जवाब की तरह लग रहा है, लेकिन आप अपने मीटर फ़ाइलें

#import "AppDelegate.h" 

और आप जोड़ना छिपा नहीं करना चाहिए में AppDelegate आयात करने के लिए नहीं है, तो कोई नेटवर्क कनेक्शन या

दिखा नहीं जोड़
0

AppDelegate.h में:

#import <iAd/iAd.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    ADBannerView *_bannerView; 
... 
... 
} 

@property (nonatomic, retain) ADBannerView *_bannerView; 

AppDelegate.m में:

@synthesize _bannerView; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
... 
    if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) { 
     self._bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner]; 
    } else { 
     self._bannerView = [[ADBannerView alloc] init]; 
    } 
... 
} 

दृश्य नियंत्रकों में जिन्हें आपको iAd जोड़ने की आवश्यकता है, नाम 'vwAd' के साथ एक कंटेनर UIView बनाएं और xib फ़ाइल में कनेक्शन बनाएं जहां आप iAds प्रदर्शित करना चाहते हैं।

@interface ViewController : UIViewController<ADBannerViewDelegate> 
{ 
    IBOutlet UIView *vwAd; 
... 
... 
} 

- (void)layoutAnimated:(BOOL)animated 
{ 
    CGRect contentFrame = self.view.bounds; 
    if (contentFrame.size.width < contentFrame.size.height) { 
     self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
    } else { 
     self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; 
    } 

    CGRect bannerFrame = self.appDelegate._bannerView.frame; 
    if (self.appDelegate._bannerView.bannerLoaded) { 
     bannerFrame.origin.y = 0; 
    } else { 
     bannerFrame.origin.y = vwAd.frame.size.height; 
    } 

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ 
     self.appDelegate._bannerView.frame = bannerFrame; 
    }]; 
} 


- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    ... 

    [self.appDelegate._bannerView removeFromSuperview]; 
    self.appDelegate._bannerView.delegate = nil; 
    self.appDelegate._bannerView.delegate = self; 
    [vwAd addSubview:self.appDelegate._bannerView]; 
    [self layoutAnimated:NO]; 
} 

कृपया ऐप्पल से iAdSuite उदाहरणों की भी समीक्षा करें। मूल लेआउटएनिएटेड फ़ंक्शन iAdSuite उदाहरणों में पाया जा सकता है। IAdSuite उदाहरण से प्रतिनिधि दृश्यों को अपने व्यू कंट्रोलर में जोड़ना न भूलें।

संबंधित मुद्दे