如何使用UITabBarController的shouldSelectViewController委托方法 [英] how to use UITabBarController's shouldSelectViewController delegate method

查看:94
本文介绍了如何使用UITabBarController的shouldSelectViewController委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

假设我单击项目1,然后正在向我显示该项目的根视图控制器.该根视图包含一个包含5个单元格的表.如果我单击一行,则将新视图推入导航堆栈.现在,此推入视图具有一个单击的按钮,它将再次推入新的视图控制器.我有4个这样的视图控制器,它们在导航堆栈中一个接一个地推送.

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.

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

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??

推荐答案

对当前选定的viewcontroller执行检查.如果current与所点击的选项卡相同,则在您的委托方法中返回no.觉得这样是您的意思吗?

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

这篇关于如何使用UITabBarController的shouldSelectViewController委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆