1

有一个问题didSelectItem UITabBar。该应用程序由 2 个视图控制器组成,通过UITabBar. 2 个标签栏:客户端 - 设置

    tabBar.delegate = self;
    NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];

    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Client" image:[UIImage imageNamed:@"icon_zakaz.png"] tag:0];
    UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Settinga" image:[UIImage imageNamed:@"icon_settings.png"] tag:1];

    [tabBarItems addObject:tabBarItem1];
    [tabBarItems addObject:tabBarItem2];

    tabBar.items = tabBarItems;
    tabBar.selectedItem = [tabBarItems objectAtIndex:0];

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{

    int selectedTag=tabBar.selectedItem.tag;
    NSLog(@"%d",item.tag);

    if (selectedTag==0)
    {
        // NSLog;
    }
    else if(selectedTag==1)
    {
        SettingsViewController *ViewController = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:nil];
        [self presentViewController:ViewController animated:NO completion:nil];
    }
}

从客户端视图控制器我可以轻松切换到设置 VC。 didSelectItem正确处理事件。

但我无法从设置切换回客户端。

似乎didSelectItem没有正确注册触摸。目前要执行此操作,我需要触摸标签栏并按住它。

请帮帮我 D:

4

2 回答 2

1

我认为你走错路了。您不应该对 tabbar 这样做,而是应该如下所示:

UITabbarController *tabbarController = [[[UITabBarController alloc] init] autorelease];

MyViewController1 *viewController1 = [[MyViewController1 alloc] initWithNibName:@"MyViewController1" bundle:[NSBundle mainBundle]];
MyViewController2 *viewController2 = [[MyViewController2 alloc] initWithNibName:@"MyViewController2" bundle:[NSBundle mainBundle]];

[tabbarController setViewControllers:[NSArray arrayWithObjects:viewController1, viewController2, nil] animated:NO];
[tabBarController setSelectedIndex:0]; // To show viewController1.

self.window.rootViewController = tabbarController;

然后 tabbar 控制器会做剩下的事情,你不必再关心 didSelectItem 了。

P/S:如果你想坚持你的选择,你应该使用 tabbar.selectedIndex 代替。

于 2013-11-13T08:46:09.283 回答
-1

试试这个,使用 item.tag

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    {

        if (item.tag == 1)
        {
            // NSLog;
        }
        else if(item.tag == 2)
        {
            SettingsViewController *ViewController = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:nil];
            [self presentViewController:ViewController animated:NO completion:nil];
        }
    }
于 2013-11-13T08:16:20.987 回答