iPhone应用程序-检测哪个选项卡栏项目被按下

发布于 2024-11-04 21:33:48 字数 485 浏览 0 评论 0原文

我有一个基于标签栏的应用程序,有超过 5 个标签栏项目 - 所以我将其中 4 个直接显示在视图中,其余的通过选择“更多”选项卡可用。当按下选项卡栏项目时,我想检测它是哪一个。
所以,在
- (void)tabBarController:(UITabBarController *)tabBarCtrl didSelectViewController:(UIViewController *)viewController 方法,我使用 tabBarCtrl.selectedViewController.title 来获取项目的标题。

这适用于视图中可见的选项卡 - 即第 4 个选项卡和“更多”选项卡 - 但不适用于按下“更多”选项卡后列表中显示的其余选项卡栏项目。

我可以看到,当从“更多”列表中选择选项卡时,甚至没有调用 didSelectViewController 方法。
当按下时我如何检测到它们中的任何一个?

先感谢您。

i have a tab bar based application, with more than 5 tab bar items - so i get 4 of them directly showing in the view and the rest available by selecting the "More" tab. When a tab bar item is pressed, i want to detect which one was it.
So, in the
- (void)tabBarController:(UITabBarController *)tabBarCtrl didSelectViewController:(UIViewController *)viewController method, i use tabBarCtrl.selectedViewController.title to get the item's title.

This works for the tabs visible in the view -that is the 4 first and the "More" tab- but does not work for the rest of my tab bar items which are shown in the list after pressing the "More" tab.

I can see that the didSelectViewController method is not even called when selecting a tab from the "More" list.
How can i detect any of them when pressed?

Thank you in advance.

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

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

发布评论

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

评论(7

陈甜 2024-11-11 21:33:48

如何获取“更多”部分中 UITabBarItem 的标题?< /a>

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}

How to get title of UITabBarItem in the More section?

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}
错爱 2024-11-11 21:33:48

您可以在 UIViewController 中使用以下代码来访问所选项目的索引。它将始终返回您的选项卡的索引。

self.tabBarController.selectedIndex;

因此,如果您有 6 个项目,您可以转到“更多...”选项卡,选择“第 5 个”项目,selectedIndex 将为 4。如果您转到“更多”选项卡并选择第 6 项,它将返回 5


编辑:如果您想检查某些 UITabBarItem 的当前位置,您可以执行以下操作:

首先,在 XIB 文件中,您应该编辑每个选项卡的 tag 属性,以便第一个选项卡选项卡将具有 tag = 100、2nd - 200、3rd - 300 等。

然后在 ViewController 中添加以下代码:

UIViewController *selectedVC = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
int selectedItemTag = selectedVC.tabItem.tag;

然后您可以使用 selectedItemTag 确定它是什么 viewController 多变的。在这种情况下,您可以通过以下方式确定 selectedIndex:selectedIndex = (selectedItemTag-100)/100

自定义 UITabBar 时,tag 属性不会更改,因此您可以信任它们:)

You can access the index of selected item by using following code in your UIViewController. It will always return yout tab's index.

self.tabBarController.selectedIndex;

So if you have e.g. 6 items you can go to the "More..." tab, select your "5th" item and selectedIndex will be 4. If you go to the More tab and select 6th item, it'll return 5.


EDIT: If you want to check current position of some UITabBarItem you can do this:

Firstly, in your XIB file you should edit the tag property of each tab, so that 1st tab will have tag = 100, 2nd - 200, 3rd - 300, etc.

Then in ViewController add this code:

UIViewController *selectedVC = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
int selectedItemTag = selectedVC.tabItem.tag;

Then you can determine what viewController is it by using selectedItemTag variable. In this case, you can determine selectedIndex by doint this: selectedIndex = (selectedItemTag-100)/100.

The tag properties are not changed when customizing your UITabBar, so you can trust them :)

玩物 2024-11-11 21:33:48

您可以使用 UITabBarDelegate 方法检测何时按下选项卡: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf /UITabBarDelegate

您可以将您的 UITabBarController 类作为委托并在实现中添加方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    NSLog(@"tab selected: %@", item.title); 
} 

You can detect when a tab has been pressed using the UITabBarDelegate methods: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITabBarDelegate

You can make your UITabBarController class be the delegate and add the method in the implementation:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    NSLog(@"tab selected: %@", item.title); 
} 
黯淡〆 2024-11-11 21:33:48

1. 因此,如果您使用 UITabBarController,您可以让该类实现 UITabBarControllerDelegate 并将 UITabBarController 委托设置为 TabBar 所选项目更改时需要通知的类,然后将委托方法添加到您的类中:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

在该方法中,您可以使用 UITabBarController selectedIndex 属性来了解当前选择的索引:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:      (UIViewController *)viewController
{
    NSLog(@"Selected index: %d", tabBarController.selectedIndex);
}

2. 如果您不只使用 UITabBar< /strong> 您可以按照 Ken Pespisa 和 iCoder 在这篇文章Ken Pespisa 和 iCoder 中的答案。

1. So if you are using a UITabBarController you can make the class implement the UITabBarControllerDelegate and set your UITabBarController delegate to the class that needs to be notified when the TabBar selected item changes, then add the delegate method to your class:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Inside this method you can use the UITabBarController selectedIndex property to know which is the current index selected:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:      (UIViewController *)viewController
{
    NSLog(@"Selected index: %d", tabBarController.selectedIndex);
}

2. If you are not using just the UITabBar you can follow the answer here by Ken Pespisa and iCoder in this post Ken Pespisa and iCoder in this post.

看轻我的陪伴 2024-11-11 21:33:48

因为您将标签添加到您的 EVERY UITabBarItem (甚至那些索引为 5 及以上的标签)。

您可以使用以下代码跟踪选择了哪个选项卡:

//MARK: - UITabBarControllerDelegate

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

    if viewController == tabBarController.moreNavigationController {
        tabBarController.moreNavigationController.delegate = self
    } else {
        setSelectedTabBarOption()
    }
}


//MARK: - UINavigationControllerDelegate

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    setSelectedTabBarOption()
}


private func setSelectedTabBarOption() {

    if let viewControllers = viewControllers {
        let selectedController: UIViewController? = viewControllers.count > selectedIndex ? viewControllers[selectedIndex] : nil
        if let tag = selectedController?.tabBarItem.tag {
            //do whatever with your tag
        }
    }
}

Since you add tags to your EVERY UITabBarItem (even those with index 5 and more).

You can track what tab was selected using following code:

//MARK: - UITabBarControllerDelegate

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

    if viewController == tabBarController.moreNavigationController {
        tabBarController.moreNavigationController.delegate = self
    } else {
        setSelectedTabBarOption()
    }
}


//MARK: - UINavigationControllerDelegate

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    setSelectedTabBarOption()
}


private func setSelectedTabBarOption() {

    if let viewControllers = viewControllers {
        let selectedController: UIViewController? = viewControllers.count > selectedIndex ? viewControllers[selectedIndex] : nil
        if let tag = selectedController?.tabBarItem.tag {
            //do whatever with your tag
        }
    }
}
梦醒时光 2024-11-11 21:33:48
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

 NSLog(@"Selected index: %d", tabBarController.selectedIndex);

if (viewController == tabBarController.moreNavigationController)
{
    tabBarController.moreNavigationController.delegate = self;
}

NSUInteger selectedIndex = tabBarController.selectedIndex;

switch (selectedIndex) {

    case 0:
        NSLog(@"click tabitem %u",self.tabBarController.selectedIndex);
        break;
    case 1:
        NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
        break;

    default:
        break;

}

}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

 NSLog(@"Selected index: %d", tabBarController.selectedIndex);

if (viewController == tabBarController.moreNavigationController)
{
    tabBarController.moreNavigationController.delegate = self;
}

NSUInteger selectedIndex = tabBarController.selectedIndex;

switch (selectedIndex) {

    case 0:
        NSLog(@"click tabitem %u",self.tabBarController.selectedIndex);
        break;
    case 1:
        NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
        break;

    default:
        break;

}

}
回梦 2024-11-11 21:33:48

如果您使用选项卡栏控制器,则应该避免了解选项卡项和视图控制器之间的映射——这是选项卡栏控制器的工作。如果您尝试将选项卡栏用于其他用途,那么您应该直接使用 UITabBar 而不是使用 UITabBarController。如果您使用 UITabBar,您可以将自己的对象设置为选项卡栏的委托,然后每当所选项目发生更改时,该委托就会收到消息。

If you're using a tab bar controller, you should avoid knowing about the mapping between tab items and view controllers -- that's the job of the tab bar controller. If you're trying to use a tab bar for something else, then you should use UITabBar directly and not use UITabBarController. If you use UITabBar, you can set your own object as the tab bar's delegate, and the delegate will then get messages whenever the selected item changes.

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