I have been working three days straight to try and integrate ads into my iPhone game. I chose mobfox and it’s been going well except a minor detail that is directly linked to my less than appropriate level of Objective-C skills.
I have everything working except that the ads get’s obscured by a UI element. So I want to bring the ads to the front and googled around and managed to figure out this:
[self.view bringSubviewToFront:bannerView];
However, that only works once as the code is in (void)viewDidLoad. I then tried to use the same code in other places of my code, in the UI init etc but I then get an error that the bannerView is not declared. I pretty much understand what xcode is telling me but I don’t have the skills to simply declare it and be done, I don’t know how to so I ask if you can help me?
All I want to do is to bring my bannerView to front at will from anywhere in the code.
Can you help me?
Best regards
Marcus
Below is the mobfox code
The following is in viewDidLoad
// MOBFOX Starts
// create the banner view just outside of the visible area
MobFoxBannerView *bannerView = [[MobFoxBannerView alloc] initWithFrame:
CGRectMake(-800, self.view.bounds.size.height - 240, 320, 50)];
bannerView.delegate = self; // triggers ad loading
//bannerView.backgroundColor = [UIColor darkGrayColor]; // fill horizontally
bannerView.transform = CGAffineTransformMakeRotation(M_PI / 2.0); //MARCUS haxx
//bannerView.refreshAnimation = UIViewAnimationTransitionCurlDown;
[self.view addSubview:bannerView];
[self.view bringSubviewToFront:bannerView];
NSLog(@"MobFox: Ad initated and placed offscreen");
//
This is directly after viewDidLoad
//MOBFOX STARTS HERE
#pragma mark MobFox Delegate
- (NSString *)publisherIdForMobFoxBannerView:(MobFoxBannerView *)banner
{
return kMyMobFoxPublisherId;
}
- (void)mobfoxBannerViewDidLoadMobFoxAd:(MobFoxBannerView *)banner
{
NSLog(@"MobFox: did load ad and placed on screen");
// animate banner into view
//[UIView beginAnimations:@"MobFox" context:nil];
//[UIView setAnimationDuration:1];
banner.frame = CGRectMake(135, 140, 320, 50);
[UIView commitAnimations];
}
- (void)mobfoxBannerView:(MobFoxBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"MobFox: did fail to load ad: %@", [error localizedDescription]);
// animate banner outside view
//[UIView beginAnimations:@"MobFox" context:nil];
//[UIView setAnimationDuration:1];
banner.frame = CGRectMake(-800, self.view.bounds.size.height - 240, 320, 50);
[UIView commitAnimations];
}
//MOBFOX ENDS HERE
Example of where I would like to call bringSubViewToFront:bannerView but can’t
-(void) settitleMenuToFront {
[self.view bringSubviewToFront:titleScreenImg];
[self.view bringSubviewToFront:highScoreTable];
[self.view bringSubviewToFront:ContinueButton];
[self.view bringSubviewToFront:highscoreButton];
[self.view bringSubviewToFront:newgameButton];
if (loadingScreen.alpha > 1) {
[self.view bringSubviewToFront:loadingScreen];
}
}
You need to add an instance variable, or ivar. The most straightforward way to do this is by adding a property and letting the compiler add the variable (the ivar) to store the property value.
In your header file, add a line:
In your implementation, add this line near the top:
Release it in
-deallocThen when you create the view, set the property:
You can then refer to self.bannerView anywhere in the rest of your code.