2012-07-29 60 views
2

這是我告訴didFinishLaunchingWithOptions有關在故事板中創建的選項卡欄控制器。它現在在主選項卡中顯示iAd標語。但是當我點擊其他標籤時沒有任何反應。 didSelectViewController永遠不會被調用。我如何將didSelectViewController連接到我的TabBarController並將currentController屬性分配給當前/活動選項卡?在AppDelegate didSelectViewController不叫

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

} 

回答

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屬性來執行此操作。

希望這有助於:)

+0

是的,謝謝,但我仍然沒有迴應didSelectViewController – ingenspor 2012-07-29 23:13:49

+0

你爲什麼這樣編程?如果你正確設置了故事板,那麼你不需要實現「didSelectViewController」。看看[本教程](http://codingandcoffee.wordpress.com/2011/10/12/iphone-tutorial-two-combining-uitabbarcontrollers-uitableviews-and-uinavigationcontrollers/) - 我認爲它可以幫助你。 – Stretch 2012-07-29 23:23:47

+0

謝謝你,我會檢查出來的,但首先它工作正常。也許我不需要didSelectViewController,但我必須告訴iAd關於currentController。我的代碼的任何建議? – ingenspor 2012-07-29 23:36:07

3

你有你的標籤視圖控制器的delegate屬性才能設置爲您AppDelegate待調用任何委託方法。

+0

@interface AppDelegate中以下行:UIResponder 像?它沒有任何區別。 – ingenspor 2012-07-29 22:53:14

+0

在尖括號中添加協議名稱意味着您的AppDelegate符合該協議..將其視爲指示您的AppDelegate現在「符合」成爲標籤欄控制器的代表。要真正設置委託屬性,請執行tabController.delegate = self;創建後 – Aky 2012-07-29 23:00:03

+0

我看不到任何區別,如果我按其他選項卡即使將self.currentController = nil;在didSelectViewController中。 NSLog(@「%s」,__FUNCTION__);沒有迴應。還有什麼建議? – ingenspor 2012-07-29 23:11:35

3

如果您從x代碼模板創建了一個標籤欄應用程序;您需要添加下面一行在你appdelegate.m

self.tabBarController.delegate=self; 

這應該走在上面的didFinishLaunchingWithOptions方法

[self.window makeKeyAndVisible]; 
相關問題