在tabBarItem Click事件上显示默认tabBarView

时间:2013-03-11 06:13:39

标签: objective-c xcode

我在我的应用中使用了UITabbarController。其中tabBarItem为contactsViewController,显示了UITableView的联系人列表。当我点击tableRow时,它会加载另一个视图。然后我点击其他tabBarItem。再点击contactsViewController它会将我带到我离开的视图。它没有显示默认的联系人视图。我已经创建了UITabbarController progrmattically.How我显示默认tab tabBarItem上的tabBarView?

tabbarController = [[UITabBarController alloc]init];
self.tabbarController.delegate = self;

tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 431, 320, 49)];

            UIImageView *tabImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 49)];
            [tabImage setImage:[UIImage imageNamed:@"Taskbar.png"]];
            [tabbarView addSubview:tabImage];

            UIButton *tabItem1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 49)];
            [tabItem1 setImage:[UIImage imageNamed:@"Btn_Home.png"] forState:UIControlStateNormal];
            [tabItem1 setTag:1];
            [tabItem1 addTarget:self action:@selector(tabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [tabbarView addSubview:tabItem1];

-(IBAction)tabBarBtnAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
//    NSLog(@"tag %d\n",btn.tag);
    [self resetTabBarBtnImage];
    [self resetAllTabBarBtnImage];
    PreviousBtnTag = btn.tag;
    if ([btn tag]==1) {
        tabbarView.hidden = YES;
        [self.tabbarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
        [btn setImage:[UIImage imageNamed:@"Btn_Home-Over.png"] forState:UIControlStateNormal];

    }
    else if([btn tag]==2)
    {
        tabbarView.hidden = NO;
        [self.tabbarController setSelectedIndex:1];
        [btn setImage:[UIImage imageNamed:@"Btn_Contacts-Over.png"] forState:UIControlStateNormal];
         [self.navigationController popToRootViewControllerAnimated:YES];

    }

2 个答案:

答案 0 :(得分:0)

appDelegate课程中,最初将UItabbarController delegate设为self

appDelegate即.m文件

的实现中
//Assuming the first tab has the contactsviewController

-

(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController   {
        if (tabBarController.selectedIndex == 0) {
            UINavigationController *requiredNavigationController = [tabBarController.viewControllers objectAtIndex:0];
            [requiredNavigationController popToRootViewControllerAnimated:YES];
        }
    }

答案 1 :(得分:0)

我建议您使用Interface Builder创建“标签栏控制器”,因为以这种方式实现这些事情要容易得多。无论如何,如果你想继续尝试以编程方式创建它,请尝试更改它:

[self.navigationController popToRootViewControllerAnimated:YES];

为此:

RootViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Root"];
    [self.navigationController pushViewController:controller animated:YES];

如果这对您不起作用,为什么不使用工具栏项而不是标签栏控制器?使用工具栏项目管理事件非常容易,因为它们被视为按钮。

祝你好运!