这就是我告诉 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/