“更多”上选定的索引UITabbar 的视图

发布于 2024-10-23 12:21:28 字数 510 浏览 5 评论 0原文

如何管理 UITabBar 的“更多”视图上的用户选择?我有这段代码来管理 UITabBarItems 选择:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {    
    if (!(viewController == tabBarController.moreNavigationController)) {
        int index = tabBarController.selectedIndex;
        [[DataManager sharedInstance] setCurrentTabbarIndex:index];
    }
}

它对于可见的 UITabBarItems 工作得很好,但是当用户从“更多”视图中选择某个项目时,我永远不会收到通知。有没有办法捕获“更多”视图的用户项目选择?谢谢!

How can I manage the user selection on the "more" view of a UITabBar? I have this code to manage the UITabBarItems selections:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {    
    if (!(viewController == tabBarController.moreNavigationController)) {
        int index = tabBarController.selectedIndex;
        [[DataManager sharedInstance] setCurrentTabbarIndex:index];
    }
}

It works fine for the visible UITabBarItems, but when the user select some item from the "more" view I never get informed about that. Is there some way to catch the user item selection of the "more" view? Thanks!

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

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

发布评论

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

评论(4

孤凫 2024-10-30 12:21:28

UITabBarController 的“更多”视图与其他视图分开处理。苹果公司对此主题的讨论如下:

[“moreNavigationController”] 属性始终包含有效的“更多”导航控制器,即使屏幕上未显示“更多”按钮也是如此。 您可以使用此属性的值在选项卡栏界面中选择更多导航控制器或将其与当前选择的视图控制器进行比较

不要手动将存储在该属性中的对象添加到标签栏界面。更多控制器会根据需要由选项卡栏控制器自动显示。您也不得在 viewControllers 属性中存储的视图控制器数组中查找“更多”导航控制器。选项卡栏控制器不包括该对象数组中的“更多”导航控制器。

由此看来,我认为你可以这样做:

int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController == 
    tabBarController.moreNavigationController) {
    index = -1;  //assign some placeholder index for the "More" controller
}

The "more" view of a UITabBarController is handled separately from the other views. Apple's discussion on this subject says the following:

[The 'moreNavigationController'] property always contains a valid More navigation controller, even if a More button is not displayed on the screen. You can use the value of this property to select the More navigation controller in the tab bar interface or to compare it against the currently selected view controller.

Do not add the object stored in this property to your tab bar interface manually. The More controller is displayed automatically by the tab bar controller as it is needed. You must also not look for the More navigation controller in the array of view controllers stored in the viewControllers property. The tab bar controller does not include the More navigation controller in that array of objects.

Judging from that I would think that you can do something like:

int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController == 
    tabBarController.moreNavigationController) {
    index = -1;  //assign some placeholder index for the "More" controller
}
蘸点软妹酱 2024-10-30 12:21:28

对于一个非常古老的问题来说,这是一个很晚的答案,但无论如何,它就在这里,以防有人偶然发现这一点。

解决方案是将您自己指定为“更多”导航控制器的委托。您已经有一个采用 UITabBarControllerDelegate 协议的类,因此 .h 文件可能如下所示:

@interface MyDelegate : NSObject <UITabBarControllerDelegate, UINavigationControllerDelegate>
{
}

无论您将类指定为委托,请执行以下操作:

- (void) assignDelegate:(MyDelegate)myDelegate toTabBarController:(UITabBarController*)tabBarController
{
  tabBarController.delegate = myDelegate;
  tabBarController.moreNavigationController.delegate = myDelegate;
}

最后,在您的委托类中添加此方法:

- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
     // add code to handle the event
}

请注意,当您以编程方式更改选项卡时,不会调用任何委托方法。

A very late answer for a very old question, but here it is anyway, in case someone stumbles upon this.

The solution is to assign yourself as the delegate of the "more" navigation controller. You already have a class that adopts the UITabBarControllerDelegate protocol, so the .h file might look like this:

@interface MyDelegate : NSObject <UITabBarControllerDelegate, UINavigationControllerDelegate>
{
}

Wherever you are assigning your class as delegate, do this:

- (void) assignDelegate:(MyDelegate)myDelegate toTabBarController:(UITabBarController*)tabBarController
{
  tabBarController.delegate = myDelegate;
  tabBarController.moreNavigationController.delegate = myDelegate;
}

And finally, in your delegate class add this method:

- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
     // add code to handle the event
}

Note that none of your delegate methods are invoked when you change tabs programmatically.

暖伴 2024-10-30 12:21:28

我发现解决这个问题的最佳方法是成为 UITableView 的委托,它是 UITabBarController.moreViewController 中的 topViewController。
实现 didSelectRowAtIndexPath 应该可以解决问题!

Stuart Sharpe 在他的博客中描述了最佳(也是最安全)的实现:http://initwithstyle.net/2014/02/making-more-of-the-more-view/

The best approach I've found for this problem is to become the delegate for the UITableView which is the topViewController in the UITabBarController.moreViewController.
Implementing the didSelectRowAtIndexPath should solve the problem!

The best (and most secure) implementation for this is described by Stuart Sharpe in his blog: http://initwithstyle.net/2014/02/making-more-of-the-more-view/

层林尽染 2024-10-30 12:21:28

无论选择什么视图,都会收到 viewWillAppear:animated:

在选项卡栏控制的每个视图中提供此信息,因此即使是从“更多”控制器进行的选择,您也可以提取用户选择的标识。

您可以在此方法中保存标签栏状态,也可以为您的视图提供对标签栏的引用并通知它。

Whatever view is being selected will receive viewWillAppear:animated:

Provide this in every view that's controlled by your tab bar, and you can thus extract the identity of the user's selection even when made from a "More" controller.

You can save the tab bar state right in this method, or you can provide your views with a reference back to the tab bar and notify it.

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