1

UITabbarController在我的应用程序中使用过。其中一个 tabBarItemcontactsViewController显示联系人列表UITableView。当我单击 tableRow 时,它会加载另一个视图。然后我单击其他一些。再次单击tabBarItemcontactsViewController会将我带到我离开的视图。它不显示默认联系人视图UITabbarController。我已经以编程方式创建。如何在点击时显示默认 tabBarView tabBarItem

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];

    }
4

2 回答 2

0

appDelegate类中,最初设置UItabbarController delegateself

appDelegateie .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];
        }
    }
于 2013-03-11T06:49:01.807 回答
0

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

[self.navigationController popToRootViewControllerAnimated:YES];

为了这:

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

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

祝你好运!

于 2013-03-11T15:16:49.587 回答