2

この値を別のクラスから出力しようとしましたが、選択したtabbarインデックスが常に 0 として表示されます

なんで?どのタブが現在選択されているかを識別する他の方法はありますか?

私の2番目のクラスのtableviewcontroller didselect`メソッドでは、次の行を印刷しようとしています

- (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);

}

しかし、それは常に0を示していますか?

別のタブに別のインデックスが必要ですか?

私の意図は、タブを識別してメソッドを呼び出すことです.4つの異なるメソッドを呼び出すように変更しますが、1つのオブジェクトを作成してメソッドを呼び出そうとすると、なぜ表示されないのですか?

私の 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:.
}
4

5 に答える 5

2

これを試して

NSUInteger index = [self.tabBarController.tabBar.items indexOfObject:self.tabBarController.tabBar.selectedItem];    
NSLog(@"Index: %d", index);
于 2013-10-25T07:49:59.230 に答える
0

このコードを試してください

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"selected tab index => %d", appDelegate.tabBarController.selectedIndex);
于 2013-05-31T05:21:39.030 に答える
0

タブバーのオブジェクトを取得して、選択したインデックスを取得できます。アプリケーションデリゲートにタブバーがあるとします。したがって、以下のコードから、選択したタブバーのインデックスを取得できます。

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%d", appDelegate.tabBarController.selectedIndex);
于 2013-05-31T05:23:22.370 に答える
0

TabbarController index change の場合、そのデリゲート プロトコルを実装する必要があります。このデリゲート プロトコルは、rootviewcontroller として実装するか、tabbarcontroller を追加する任意の viewcontroller 内に追加する必要があります。

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

}

タブバーのすべてのインデックスで、異なるビューコントローラーを実装する必要があり、すべての子ビューコントローラーのviewWillAppear内で、そのインデックス値をログに記録できます。

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

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

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


}

お役に立てば幸いです。

于 2013-05-31T05:24:57.043 に答える