2

This is how I tell didFinishLaunchingWithOptions about the Tab Bar Controller created in storyboard. It now displays the iAd banner in the main tab. But nothing happens when I tap the other tabs. didSelectViewController is never being called. How can I connect didSelectViewController to my TabBarController and assign currentController property to current/active tab?

#import "AppDelegate.h"

@interface AppDelegate()

@property (nonatomic, retain) ADBannerView *bannerView;
@property (nonatomic, assign) UIViewController<BannerViewContainer> *currentController;

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize bannerView;
@synthesize currentController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch

    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    // bannerView was here

    // This is the reference to the tab bar controller and first tab from storyboard.
    UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
    self.currentController = [[tabController viewControllers] objectAtIndex:0];

    NSLog(@"Root: %@", self.window.rootViewController);
    NSLog(@"Tab with banner: %@", self.currentController);

    return YES;
}

//and this isn't called:

- (void)tabController:(UITabBarController *)tabController didSelectViewController:
(UIViewController *)viewController
{
    // If called for selection of the same tab, do nothing
    if (currentController == viewController) {
        return;
        }
    if (bannerView.bannerLoaded)  {
     // If we have a bannerView atm, tell old view controller to hide and new to show.
        [currentController hideBannerView:bannerView];
        [(UIViewController<BannerViewContainer>*)viewController showBannerView:bannerView];
        }
        // And remember this viewcontroller for the future.
    self.currentController = (UIViewController<BannerViewContainer> *)viewController;

}
ingenspor
  • 924
  • 2
  • 15
  • 37

3 Answers3

9

As omz eluded to, you need to set the delegate of the UITabBarController - add the third line, below:

// This is the reference to the tab bar controller and first tab from storyboard.
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
tabController.delegate = self;

What you have done by doing this: @interface AppDelegate : UIResponder <UIApplicationDelegate, ADBannerViewDelegate, UITabBarControllerDelegate> is saying, "My class AppDelegate conforms to the protocol for UITabBarControllerDelegate". However, the tab bar controller instance does not know about your class yet. So you have to tell the tab bar controller which AppDelegate instance is the one that you want it to make call backs to. You do this by setting the delegate property.

hope this helps :)

Stretch
  • 3,669
  • 2
  • 28
  • 40
  • Yes thanks, but I still get no response to didSelectViewController – ingenspor Jul 29 '12 at 23:13
  • why are you doing this programatically? If you setup your storyboard correctly, then you don't need to implement "didSelectViewController". Check out [this tutorial](http://codingandcoffee.wordpress.com/2011/10/12/iphone-tutorial-two-combining-uitabbarcontrollers-uitableviews-and-uinavigationcontrollers/) - I think it might help you. – Stretch Jul 29 '12 at 23:23
  • Thank you, I will check it out, but it's working fine in the first place. Maybe I won't need the didSelectViewController, but I have to tell the iAd about the currentController. Any suggestions for my code? – ingenspor Jul 29 '12 at 23:36
  • You could move the banner code that you have in `didSelectViewController` into the `viewDidAppear` method of all the UIViewControllers that you have linked to each tab. Then, when the user selects a tab, and that view controller appears, the `viewDidAppear` gets called and you can do your banner business there instead. – Stretch Jul 30 '12 at 00:03
  • Thanks, if you want a Bounty and lots of rep you are welcome to show me that in this question: [iad in Storyboard TabBarController](http://stackoverflow.com/questions/11655953/shared-iad-banner-for-uitabbarcontroller-based-app-using-xcode-4-with-storyboard) – ingenspor Jul 30 '12 at 00:15
3

You have to set your tab view controller's delegate property to your AppDelegate in order for any delegate methods to be called.

omz
  • 53,243
  • 5
  • 129
  • 141
  • @interface AppDelegate : UIResponder like that? It didn't make any difference. – ingenspor Jul 29 '12 at 22:53
  • Adding the protocol name in the angle brackets just means that your AppDelegate conforms to that protocol.. think of it as indicating that your AppDelegate is now "eligible" to be a tab bar controller's delegate. To actually set the delegate property, do tabController.delegate = self; after you create it – Aky Jul 29 '12 at 23:00
  • I can't see any difference after this, if I press other tabs nothing happens even if I put self.currentController = nil; in didSelectViewController. NSLog(@"%s", __FUNCTION__); is not responding. Any further suggestions? – ingenspor Jul 29 '12 at 23:11
3

If you created a tab bar application from the x-code template; you need to add the following line in your appdelegate.m

self.tabBarController.delegate=self;

This should go above the following line in the didFinishLaunchingWithOptions method

[self.window makeKeyAndVisible];