iphone - AppDelegate 中的 didSelectViewController 没有被调用

标签 iphone objective-c xcode delegates uitabbarcontroller

这就是我告诉 didFinishLaunchingWithOptions 关于 Storyboard中创建的选项卡栏 Controller 的方式。现在,它会在主选项卡中显示 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;

}

最佳答案

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

// 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 的协议(protocol)”。但是,选项卡栏 Controller 实例还不知道您的类。因此,您必须告诉选项卡栏 Controller 哪个 AppDelegate 实例是您希望其回调的实例。您可以通过设置delegate来做到这一点属性(property)。

希望这有帮助:)

关于iphone - AppDelegate 中的 didSelectViewController 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11713614/

相关文章:

iphone - 无法将 iOS/iPhone 应用程序构建为 i386 以外的任何应用程序

XCode 中的 iPhone jQuery mobile + PhoneGap .. 具有用于多个 View /页面的多个 html 文件

ios - 在 UIWebView 中限制导航

objective-c - objective-c : UITableView Scrolling becomes Laggy

ios - 全局访问 UIalertview 中的文本字段数据

ios - 使用嵌入式 HTTP 代理缓存流视频?

iphone - 如何使带有 SLComposeViewController 的 presentViewController 更快?

iphone - 远程通知 token 处理

ios - Xcode 向后兼容性

iOS mapview 使用 word 搜索位置