【问题标题】:TabBarController isKindOfClass - problem with UINavigationControllerTabBarController isKindOfClass - UINavigationController 的问题
【发布时间】:2023-04-05 12:44:01
【问题描述】:

我有一个带有 ViewController 的 TabBar。我在我的 AppDelegate 中执行此操作。所以我有一个 UINavigationController

test1ViewController = [[Test1ViewController alloc] init];
test2ViewController = [[Test2ViewController alloc] init];
test3ViewController = [[Test3ViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: test2ViewController];

NSArray* controllers = [NSArray arrayWithObjects: test1ViewController, navigationController, test3ViewController, nil];
[self.tabBarController setViewControllers:controllers animated:YES];

[navigationController release];

现在我遇到了这行源代码的问题:

[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];

这里会有一个 SIGBRT,因为 selectedViewController 在这种情况下是一个“UINavigationController”。但我想调用“Test2ViewController”的方法。我怎么能这样做? 通常我也会这样做:

if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }

但这也失败了,因为它是一个 UINavigationController。如何解决?有人知道吗?

非常感谢提前和最好的问候。

【问题讨论】:

    标签: iphone uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    尝试以下方法:

    UINavigationController   *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
    Test2ViewController      *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];
    
    [viewController methodName:arg1 withTag:arg2];
    

    【讨论】:

    • 不起作用:警告:“UINavigationController”可能无法响应“-rootViewController”。 SIGABRT 也在这一行发生。
    • 对不起,尝试修改代码:[[navController viewControllers] objectAtIndex: 0];
    • 行得通,但我可以用一行来完成吗: [[[[appDelegate.myTabBarController selectedViewController] viewControllers] objectAtIndex: 0] methodName:arg1 withTag:arg2];这可行,但出现警告:“UIViewController”可能无法响应“-viewControllers”是否可以避免此警告消息?非常感谢!
    • 我们不是在尝试向 UIViewController 发送 viewControllers 消息,而是在尝试将其发送到 UINavigationController;检查你的括号和括号。
    • 哦,是的,我明白我的错误了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多