0

我試圖從另一個類打印這個值,但它始終顯示所選的tabbar索引爲0爲什麼Tabbar選擇的項目總是0?

爲什麼?有沒有其他方法可以確定現在選擇哪個選項卡?

在我的第二類tableviewcontrollerdidselect`方法,我試圖打印以下行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    sharedManager=[Mymanager sharedManager]; 
    sharedManager.navigationBarTitle=[name objectAtIndex:indexPath.row]; 
    NSLog(@"%d",self.tabBarController.selectedIndex); 

} 

,但它始終顯示?

我需要不同的索引不同的標籤?

我的意圖是確定選項卡和調用方法現在我改變,調用四個不同的methodes 但當我創建一個對象,並試圖調用方法,它不顯示爲什麼?

我APpdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [NSThread sleepForTimeInterval:5.0]; 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"internet connection" object:self]; 
    dispatch_queue_t connectivityThread = dispatch_queue_create("com.ttt.test.connectivity", NULL); 

    dispatch_async(connectivityThread, ^{ 
     while (true){ 
      if([GMMConnectivity hasConnectivity]) 
          [[NSNotificationCenter defaultCenter] postNotificationName:@"InternetCheck" object:[NSNumber numberWithBool:YES]]; 
      else 
       [[NSNotificationCenter defaultCenter] postNotificationName:@"InternetCheck" object:[NSNumber numberWithBool:NO]]; 

      usleep(5000000); 
     } 
    }); 

    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 


} 
//-(void)recentPages:(NSString *)pageNumber 
//{ 
// //NSLog(@"%@",pageNumber); 
//} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 
+0

你肯定self.tabBarController不是零? – rdelmar

+0

它的0 ..值是0 – Navi

+0

我敢肯定你錯誤'UITabBar' UITableView'。 – Cyrille

回答

0

試試這個代碼

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSLog(@"selected tab index => %d", appDelegate.tabBarController.selectedIndex); 
+0

是否有效?讓我試試 – Navi

+0

iam在我的appdelegate類的對象類型中找不到類似@property tabbarcontroller的錯誤 – Navi

+0

@Diy u there?你好? – Navi

0

你可以採取的TabBar &的對象都被選擇它的索引。假設你在應用程序委託中有tabbar。所以,從下面的代碼你可以得到tabbar的選擇索引。

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSLog(@"%d", appDelegate.tabBarController.selectedIndex); 
+0

iam使用故事板將無法正常工作? – Navi

+0

您可以嘗試使用Tabbar對象。它應該工作。 –

0

對於TabbarController索引更改,您需要實現其委託協議。不同的視圖控制器,每個子視圖控制器的內部viewWillAppear中 - 你必須實現不同的這種委託協議,應落實爲RootViewController的或添加任何視圖 - 控制內部在其中添加您的tabbarcontroller

#pragma mark - 
#pragma mark - Tab bar Delegate 

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 

    int i=tabBarController.selectedIndex; 

    NSLog(@"selected tab index => %d", i); 

    switch (i) { 

     case 0: 
      [tabBarController setSelectedIndex:i]; 
      break; 

     case 1: 
      [tabBarController setSelectedIndex:i]; 
      break; 

     case 2: 
      [tabBarController setSelectedIndex:i]; 
      break; 

     case 3: 
      [tabBarController setSelectedIndex:i]; 
      break; 

     default: 
      break; 
    } 

} 

在標籤欄的各項指標您可以記錄其索引值。

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    NSLog(@"selected tab value=> %d", appDel.tabBarController.selectedIndex); 


} 

我希望它可以幫助你。

+0

在我的tabbarcontains四個viewcontrooler我應該添加 – Navi

0

如果您想要獲取選定的項目並在Tabbar委託中設置相同的變量,請在您的類中創建一個變量,使其成爲accessbile。

@interface iPadAppDelegate : NSObject < UITabBarControllerDelegate> 

(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 

(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 

希望這有助於

+0

請告訴我,我把這個? – Navi

+0

在appdelegate中聲明一個變量,或者在您需要的方法中可訪問的任何其他類中聲明變量。 – Ekra

+0

好的,那麼上面的方法呢? – Navi

0

嘗試這個

NSUInteger index = [self.tabBarController.tabBar.items indexOfObject:self.tabBarController.tabBar.selectedItem];  
NSLog(@"Index: %d", index); 
相關問題