繁体   English   中英

在UITabBarController中实现Back操作

[英]Implement Back action into UITabBarController

我有一个controllerView(MenuControllerView),其中带有一个按钮,当我单击该按钮时,将出现一个新的ViewController,并以编程方式创建一个TabBarController,如下所示:


UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];

tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];

我不想在第一个按钮Item上显示ViewController1,而是要在其中放一个操作以返回到MenuViewController,但是我不知道该怎么做。

谢谢

您是否考虑过将UITabBarController呈现为模式视图控制器并实现UITabBarControllerDelegate? 例如,这似乎对我有用(我在这里将第三个选项卡返回到MenuViewController):

@interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...

- (IBAction) onButtonPressed:(id)sender
{

    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    viewController1 = [[ViewController1 alloc] init];
    viewController2 = [[ViewController2 alloc] init];
    viewController3 = [[ViewController3 alloc] init];

    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
    [[self tabBarController] setSelectedIndex:1];

    tabBarController.delegate = self;
    [self presentModalViewController:tabBarController animated:NO];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
    if (viewController == viewController3)
    {
        [self dismissModalViewControllerAnimated:NO];
        return NO;
    }
    return YES;
}

如果我理解正确,则可以从超级视图中删除标签栏的视图。

 [[tabBarController view] removeFromSuperview]; 

如果只想处理选项卡项的选择,则可以使用UITabBarDelegate协议的tabBar:didSelectItem:方法。

我怀疑这种方法是否好。 您将打破典型的iPhone行为,这会使用户感到困惑。 TabBarController设计为(在功能上和技术上)在视图之间进行切换,而NavigationController用于推入和弹出视图(来回)。 当然,您可以将它们组合在一起(这并不总是那么容易),但是您不应该将TabBar用作NavigationBar。

这是您要做什么?

替代文字

在推送到子视图控制器时,将使用UINavigationController自动创建此控件。

[self.navigationController pushViewController:yourChildViewController animated:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM