确定 UITabBar Item 属于哪个 UIViewController 子类

发布于 2024-09-19 09:42:37 字数 958 浏览 5 评论 0原文

我在 appDelegate 中实例化了一个 UITabBarController,但我需要一种方法来知道用户何时按下不同的选项卡栏项目(选项卡栏上的按钮)。

-UITabBarDelegate 协议来救援(使用所需的 didSelectViewController 方法)!

在 Interface Builder 中连接所有内容后,如何获取对与按下的此选项卡栏项目相对应的实际 UIViewController 子类实例的引用?

我需要这个引用,因为每次按下选项卡栏项目时,我都需要调用 UIViewControllers 子类之一中的方法。

有什么建议吗?

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{   
    NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here     

    if ([theTabBarController selectedIndex] == 1) {         
        MySecondViewController *reference = (MySecondViewController *) viewController;      

    if ([reference isKindOfClass:[UINavigationController class]]) {
        NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
    }   
}

I have a UITabBarController instansiated in my appDelegate, but I need a way to know when the user presses the different tab bar items (buttons on the tab bar).

-UITabBarDelegate protocol to the rescue (with the required didSelectViewController-method)!

With everything wired up in Interface Builder, how do I obtain a reference to the actual UIViewController subclass instance that corresponds to this tab bar item that was pressed?

I need this reference because I need to call a method in one of my UIViewControllers subclasses every time that tab bar item is pressed.

Any suggestions?

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{   
    NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here     

    if ([theTabBarController selectedIndex] == 1) {         
        MySecondViewController *reference = (MySecondViewController *) viewController;      

    if ([reference isKindOfClass:[UINavigationController class]]) {
        NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
    }   
}

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

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

发布评论

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

评论(1

分開簡單 2024-09-26 09:42:37

可能我没有正确理解你的问题,但似乎你所要求的只是“viewController”参数,该参数被传递到你提到的方法调用中

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

UITabBarController 也有一个属性来获取相同的信息

@property(nonatomic, assign) UIViewController *selectedViewController

Possibly I am not understanding your question correctly, but it seems that what you're asking for is simply the "viewController" parameter which is passed into the method call you've mentioned

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

UITabBarController also has a property to get the same info

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