iAd Banner View Delegate Not Calling Methods - objective-c

I have an iAd banner view, with all my contracts up and running, and I've implemented the ADBannerView delegate. The banner should disappear with no internet connection, but it just shows a white box where the content should be. I know I have all the code right, I've seen a million tutorials on this. So I ran some tests and found that the banner view wasn't even calling the two methods for the delegate! Here is the code.
In the .h file:
#import <iAd/iAd.h>
#interface DetailViewController : ADBannerViewDelegate>
{
ADBannerView *aBanner;
BOOL bannerIsVisible;
}
#property (nonatomic, retain) IBOutlet ADBannerView *aBanner;
#property (nonatomic, assign) BOOL bannerIsVisible;
#end
in the .m file:
#implementation DetailViewController
#synthesize aBanner,bannerIsVisible;
//Show banner if can load ad.
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL]; banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations]; self.bannerIsVisible = YES; }
}
//Hide banner if can't load ad.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL]; banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations]; self.bannerIsVisible = NO; }
}

You have to set the banner delegate to files owner. I had the same issue and after beating my head against the wall it was that simple.

Use the code shown below to declare your own delegate
-(void)viewDidLoad {
aBanner.delegate = self;
}

Related

popup can't close (IBAction not working)

I use a popup system that I found here : https://blog.typpz.com/2013/12/09/ios-sdk-create-a-pop-up-window/
And after following the guide step by step everything works except the closing button. I tried to link it in various ways but nothing works.
Does anyone know what I do wrong ?
here is my code:
PopUpController.h:
#import <UIKit/UIKit.h>
//#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#interface PopUpViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *popUpView;
- (void)showInView:(UIView *)aView animated:(BOOL)animated;
- (IBAction)closePopup:(id)sender;
#end
PopUpController.m:
#import "PopUpViewController.h"
#interface PopUpViewController ()
#end
#implementation PopUpViewController
- (void)viewDidLoad {
self.view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
self.popUpView.layer.cornerRadius = 5;
self.popUpView.layer.shadowOpacity = 0.8;
self.popUpView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.popUpView.clipsToBounds = YES;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma animation
- (void)showAnimate
{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.view.alpha = 0;
[UIView animateWithDuration:.25 animations:^{
self.view.alpha = 1;
self.view.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)removeAnimate
{
[UIView animateWithDuration:.25 animations:^{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.view.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[self.view removeFromSuperview];
}
}];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
- (IBAction)closePopup:(id)sender {
NSLog(#"closing...");
[self removeAnimate];
}
#end
NOTE: I don't even get the NSLog closing... on my console when I click the closing button
here is a screenshot of my .xib file:
Thank you for your response.

Passing data and delegation

I have got a question about delegation. I am fairly new to programming and I have just managed my first exercise-project in which I use delegation. All it is, is a Parent View Controller that calls a Child View Controller which has a text field and a button in it. I write something in that text field, press the button, the Child VC is dismissed and it passes the text back. It then gets displayed in a label on the Parent. I have got a couple of animations for the transition. It all works. Here are some pictures first and the code:
This is my parent view controller:
Click the button and the Child appears:
Write something in the text field and press the button to pass it back:
The data is displayed on a label in the parent view controller.
Here are the files for this:
ParentViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
#interface ParentViewController : UIViewController <ChildViewControllerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
ParentViewController.m
#implementation ParentViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)Pressed:(id)sender {
ChildViewController *childViewController = [[ChildViewController alloc]init];
[self displayContentController:childViewController];
childViewController.delegate = self;
}
- (void) displayContentController: (UIViewController*) content {
[self addChildViewController:content];
content.view.frame = CGRectMake(0, 115, 320, 240);
content.view.backgroundColor = [UIColor redColor];
CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[content.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void) passDataBack:(NSString *)data{
self.label.text = data;
}
#end
ChildViewController.h
#import <UIKit/UIKit.h>
#protocol ChildViewControllerDelegate <NSObject>
-(void)passDataBack:(NSString*)data;
#end
#interface ChildViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *txtfield;
#property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)passingBack:(id)sender;
#property NSString *data;
#end
ChildViewController.m
#import "ChildViewController.h"
#interface ChildViewController ()
#end
#implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)passingBack:(id)sender {
NSString *itemToPassBack = self.txtfield.text;
[self.delegate passDataBack:itemToPassBack];
[self.childViewControllers lastObject];
[self willMoveToParentViewController:nil];
[UIView animateWithDuration:1
delay:0.0
usingSpringWithDamping:1
initialSpringVelocity:1
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.frame = CGRectMake(-320, 115, 320, 240);
} completion:^(BOOL complete){
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
}
#end
Regarding the code, there are a couple of things I am not sure about, as I am learning I copied a lot of code etc... so for example I am not sure as to how to animate the child controller back using the same method as I used to animate it in, or if it matters and what is best practice etc... also, I get a strange message in the console: "Unknown class ViewController in Interface Builder file." but it works, so I am not sure why it's there. For the record, I haven't used the storyboard, only xibs.
My question though is the following: I would like to have the button to dismiss the Child View Controller not in the Child View Controller but in the parent, like so:
And that's where I am lost. I just don't know how to trigger the method to pass data back from the Parent View Controller. Can anyone help? many thanks

Transition from one UIViewController to another in a custom segue

This custom segue visually works correctly but there is an warning when completed.
Warning: Attempt to present <DestViewController: 0x21059f40> on <SrcViewController: 0x1fd50cf0> whose view is not in the window hierarchy!
Any help on how to get this to work without the warning would be much appreciated, have spent more time on this than i should. Not even sure if I should care about it since it is a warning.
- (void)perform {
UIViewController *src = (UIViewController *)self.sourceViewController;
UIViewController *dst = (UIViewController *)self.destinationViewController;
dst.view.alpha = 0;
[UIView animateWithDuration:0.5
animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[src.view addSubview:dst.view];
dst.view.alpha = 1;
}
completion:^(BOOL finished){
[dst.view removeFromSuperview];
//need to understand why this throws a warning but still works
[src presentViewController:dst animated:NO completion:^(){
[src reset];
}];
}];
}
UPDATE
There was a call to stop a MPMoviePlayerController in the reset method on the src UIViewController. For some reason that was causing the issue, once removed this segue works perfectly. See my answer below for final implementation of segue.
You are removing the view from superview i.e src.view and presenting the same viewController after it , so it is showing warning.
Try this (If this doesn't work tell me):
[UIView animateWithDuration:0.5
animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
dst.view.alpha = 1;
}
completion:^(BOOL finished){
[src presentViewController:dst animated:NO completion:^(){
[src reset];
}];
}];
}
although the original issue was caused by something outside of the segue, as mentioned above, i thought i would post the full working segue code for others who may end up here. The delegate in this example is a custom UIViewController container where the child UIViewContainers where added as children in the following way:
[self addChildViewController:childController_];
[self.view addSubview:childController_.view];
segue.h
#import <UIKit/UIKit.h>
#interface COFSimpleSegue : UIStoryboardSegue
#property (assign) UIViewController *delegate;
#end
segue.m
#import "COFSimpleSegue.h"
#import <QuartzCore/QuartzCore.h>
#implementation COFSimpleSegue
#synthesize delegate = delegate_;
- (void)perform {
UIViewController *src = (UIViewController *)self.sourceViewController;
UIViewController *dst = (UIViewController *)self.destinationViewController;
dst.view.frame = delegate_.view.bounds;
dst.view.autoresizingMask = delegate_.view.autoresizingMask;
[src willMoveToParentViewController:nil];
[delegate_
transitionFromViewController:src
toViewController:dst
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void){}
completion:^(BOOL finished) {
[dst didMoveToParentViewController:delegate_];
[src removeFromParentViewController];
}
];
}
#end

How would I implement a splash screen that animates away?

This is what I have so far:
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
-(void)animate
{
[UIView animateWithDuration:1 animations:^{
splashImage.frame = CGRectMake(0,480,320,480);
}];
[UIView commitAnimations];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splashImage = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"carolinaWolf.png"]];
splashImage.frame = CGRectMake(0, 0, 320, 480);
[self.tabBarController.view addSubview:splashImage];
[self animate];
return YES;
}
My initial view in storyboard is a TabBarController. What I want to happen is: after I finish loading new content into the application, I want to splash screen to animate down and off the screen. This worked before I started using storyboard, why won't it work now?
My AppDelegate.h looks like this:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
UIImageView *splashImage;
You can directly just add your splashImage to self.window.rootViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImage *splashImage = [UIImage autoAdjustImageNamed:#"Default.png"];
UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
[self.window.rootViewController.view addSubview:splashImageView];
[self.window.rootViewController.view bringSubviewToFront:splashImageView];
[UIView animateWithDuration:1.5f
delay:2.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
splashImageView.alpha = .0f;
CGFloat x = -60.0f;
CGFloat y = -120.0f;
splashImageView.frame = CGRectMake(x,
y,
splashImageView.frame.size.width-2*x,
splashImageView.frame.size.height-2*y);
} completion:^(BOOL finished){
if (finished) {
[splashImageView removeFromSuperview];
}
}];
return YES;
}
This is how I fade away my splash screens:
#pragma mark Fading Default.png Animation
#interface FadeDefault : UIViewController
#end
#implementation FadeDefault
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
/*if ( IDIOM == IPAD ) /* Optional */
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default-Portrait~ipad.png"]];
[fader setFrame:CGRectMake(0,0,768,1024)];
}else
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
}*/
self.view = fader;
[fader release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(imageDidFadeOut:finished:context:)];
self.view.alpha = 0.0;
[UIView commitAnimations];
}
- (void)imageDidFadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[self.view removeFromSuperview];
}
#end
#pragma mark - Application lifecycle
Keep in mind this is an example, although code I actually use and I know works, you need to keep your other didFinishLaunchingWithOptions code in place. Just add the fader code to you existing code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* custom fading Default.png animation */
FadeDefault * fader = [[FadeDefault alloc] init];
[window addSubview:navigationController.view];
[window addSubview:fader.view];
[fader release];
[window makeKeyAndVisible];
return YES;
}

Animation between two UIViewController when I do makeKeyAndVisible

I'm trying to do an animation to switch between two UIViewController that I have. But when the animation runs, the old UIViewController is white.
// viewController is the new UIViewController
[UIView animateWithDuration:0.6
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^(void){
[viewController.view setTransform:CGAffineTransformMakeTranslation(0, -460)];
}
completion:^(BOOL finished){
if (finished){
//
}
}
];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:viewController];
[[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
I have an object which always has the UIViewController:
#interface ManagerViews : NSObject {
UIViewController *oneController;
UIViewController *twoController;
...
}
#property(nonatomic, retain) UIViewController *oneController;
#property(nonatomic, retain) UIViewController *twoController;
...
What am I doing wrong?
Have you tried to set the old UIViewController to "nil" or (my Objective-C expertise is a little rusty) reset?