2

这就是我告诉 didFinishLaunchingWithOptions 故事板中创建的标签栏控制器的方式。它现在在主选项卡中显示 iAd 横幅。但是当我点击其他选项卡时没有任何反应。didSelectViewController 永远不会被调用。如何将 didSelectViewController 连接到我的 TabBarController 并将 currentController 属性分配给当前/活动选项卡?

#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;

}
4

3 回答 3

9

正如 omz 所逃避的那样,您需要设置UITabBarController- 添加第三行的委托,如下所示:

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

你这样做@interface AppDelegate : UIResponder <UIApplicationDelegate, ADBannerViewDelegate, UITabBarControllerDelegate>是说,“我的类 AppDelegate 符合 UITabBarControllerDelegate 的协议”。但是,标签栏控制器实例还不知道您的类。因此,您必须告诉标签栏控制器哪个 AppDelegate 实例是您希望它进行回调的实例。您可以通过设置delegate属性来做到这一点。

希望这可以帮助 :)

于 2012-07-29T23:01:26.243 回答
3

您必须将选项卡视图控制器的delegate属性设置为您AppDelegate的,以便调用任何委托方法。

于 2012-07-29T22:50:05.040 回答
3

如果您从 x-code 模板创建了一个标签栏应用程序;您需要在 appdelegate.m 中添加以下行

self.tabBarController.delegate=self;

这应该高于 didFinishLaunchingWithOptions 方法中的以下行

[self.window makeKeyAndVisible];
于 2013-02-15T10:17:01.327 回答