OGeek|极客世界-中国程序员成长平台

标题: iphone - 检测标签栏选择的 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:00
标题: iphone - 检测标签栏选择的 View Controller

我使用标签栏应用程序和 Storyboard 创建了一个项目。

现在我有了 FirstViewController(默认选择)和 SecondViewController。

如何捕捉所选 View Controller 发生变化的时刻? (用户在标签栏上选择第二个 View Controller )。

我想到的唯一方法是: 1.在appDelegate中为UITabBarController创建一个属性。

  1. 使用 self.tabBarController = (UITabBarController *)self.window.rootViewController

  2. 使第一个 View Controller 符合 UITabBarControllerDelegate。

  3. 将标签栏的委托(delegate)设置为 FirstViewController 使用 [[UIApplication sharedApplication] delegate]

有没有更简单更干净的方法来做到这一点?



Best Answer-推荐答案


我不遵循第三步,但这是我认为你想要做的:

子类标签栏 Controller 并实现委托(delegate)协议(protocol):

@interface RootTabBarController : UITabBarController

并在它自己的 viewDidLoad 中设置它的 delegate = self。实现:

- (void)tabBarControllerUITabBarController *)tabBarController didSelectViewControllerUIViewController *)viewController

关于iphone - 检测标签栏选择的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15706734/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4