在 UITabBarController 中实现 Back 操作

发布于 2024-08-17 20:41:44 字数 794 浏览 1 评论 0原文

我有一个控制器视图(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]];

我不想为第一个按钮项显示 ViewController1,而是想将一个操作放回其中返回到我的 MenuViewController,但我不知道该怎么做。

谢谢

I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:


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

Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莳間冲淡了誓言ζ 2024-08-24 20:41:44

您是否考虑过将 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;
}

Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):

@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;
}
辞别 2024-08-24 20:41:44

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

I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.

明天过后 2024-08-24 20:41:44

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

[[tabBarController视图]removeFromSuperview];

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

if I understand right, you can just remove your tabbar's view from superview. smth like

[[tabBarController view] removeFromSuperview];

if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.

衣神在巴黎 2024-08-24 20:41:44

这就是你想做的吗?

替代文本

这是在推送到子视图控制器时使用 UINavigationController 自动创建的。

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

Is this what you're trying to do?

alt text

This automatically created with a UINavigationController upon pushing to a child view controller.

[self.navigationController pushViewController:yourChildViewController animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文