如何使用UITabBarController的shouldSelectViewController委托方法

发布于 2024-12-22 12:48:00 字数 440 浏览 1 评论 0原文

我有一个基于选项卡栏的应用程序(选项卡栏控制器添加在窗口本身中),并且所有导航控制器及其各自的根视图控制器都在窗口的 xib 中设置。我有 4 个选项卡栏项目。

假设我单击第 1 项,则将向我显示该项目的根视图控制器。该根视图包含一个包含 5 个单元格的表。如果我单击一行,则会将一个新视图推送到导航堆栈上。现在,这个推送的视图有一个按钮,单击该按钮将再次推送一个新的视图控制器。我有 4 个这样的视图控制器,它们被一个接一个地推送到导航堆栈上。

现在,假设我位于导航堆栈中的第三个视图,然后,我单击了选项卡栏项目 1(与我之前单击的相同);然后,显示第一个根视图控制器,我的整个导航堆栈消失了。我只是不想发生这种情况,也就是说,我想保留在第三个视图控制器上,并且能够单击所有选项卡栏项目(不想禁用任何项目)。我知道它可以通过实现选项卡栏控制器委托方法来实现:shouldSelectViewController,但我不知道如何实现?

I have a tabbar-based application (tabbar controller is added in window itself) and all the navigation controller with their respective root view controllers are being set in window's xib. I have 4 tab bar items.

Suppose I click on item 1, then the root view controller for that item is being shown to me. This root view contains a table with 5 cells. If I click on a row, then a new view is pushed onto the navigation stack. Now, this pushed view has a button clicking on which will again push a new view controller. I have 4 such view controllers which are getting pushed one after the other on navigation stack.

Now, lets say I am on 3rd view in navigation stack and then, I have clicked on tab bar item 1 (the same on which I clicked earlier); then, the first root view controller is shown and my whole navigation stack is gone. I just don't want this to happen, that is, I want to remain on the 3rd view controller and also, be able to click on all tab bar items (dont want to disable any item). I know that it can be achieved through implementing tab bar controllers delegate method: shouldSelectViewController, but i dont know how??

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

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

发布评论

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

评论(1

等风也等你 2024-12-29 12:48:00

对当前选定的视图控制器执行检查。
如果当前与点击的选项卡相同,则在委托方法中返回 no 。你认为这样的事情就是你的意思吗?

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{
if ([[tabBarController viewControllers] objectAtIndex:tabBarController.selectedIndex] == viewController)
    {
    return NO;
    }
else
    {
    return YES;
    }   
}

perform a check for the currently selected viewcontroller.
if current is the same as the tab tapped, then return no in your delegate method. Think something like this is what you mean?

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{
if ([[tabBarController viewControllers] objectAtIndex:tabBarController.selectedIndex] == viewController)
    {
    return NO;
    }
else
    {
    return YES;
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文