为什么Tabbar选中的项目总是0?

时间:2013-05-31 05:18:13

标签: ios objective-c methods uiviewcontroller uitabbarcontroller

我尝试从另一个类打印此值,但它总是将选定的tabbar索引显示为0

为什么呢?有没有其他方法来确定现在选择哪个标签?

在我的第二课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

我需要不同标签的不同索引?

我的目的是识别标签和调用方法,现在我改变它来调用四种不同的方法 但是当我创建一个对象并尝试调用方法时它不显示原因?

我的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:.
}

5 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

试试此代码

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

答案 2 :(得分:0)

您可以使用tabbar&的对象得到它的选定索引。假设您在应用程序委托中有tabbar。因此,从下面的代码中,您可以获得tabbar的选定索引。

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

答案 3 :(得分:0)

对于TabbarController索引更改,您需要实现其委托协议。这个委托协议应该作为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);


}

我希望它可以帮到你。

答案 4 :(得分:0)

如果要获取所选项目并在tabbar委托中设置相同的变量,请创建一个变量,以便在类中可以访问它。

@interface iPadAppDelegate : NSObject < UITabBarControllerDelegate>

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

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

希望这有帮助