gpt4 book ai didi

ios - 检查特定的 UIViewController 是否在 TabBarItems 中

转载 作者:行者123 更新时间:2023-11-29 03:17:37 25 4
gpt4 key购买 nike

使用 UITabBarController 我正在尝试以编程方式添加或删除某些 ViewControllers 作为选项卡,无论用户是否登录。

我使用以下代码添加 ViewController“SecondViewController”:

[newTabs addObject:second];
[self.tabBarController setViewControllers:newTabs];

然后,我运行下面的代码来检查特定的 ViewController(第二个)是否在数组中。但它不起作用:vs 永远不等于第二。

NSMutableArray *newTabs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
BOOL found = FALSE;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
for (UIViewController *vc in newTabs){
if([vc isEqual: second]){
found=TRUE;
}
}

当我执行 NSLog 时,这是响应:

2014-02-01 18:38:18.755 App Login Test[1469:11303] PostData: <SecondViewController: 0x71a2830>
2014-02-01 18:38:18.756 App Login Test[1469:11303] PostData: <SecondViewController: 0x75bcd90>

(这是我运行 vc 和第二个 NSLog 的时候,比较两者,并理解为什么它们不相等。)

我已经找了一段时间了,但找不到对此的解释! found 应该设置为 TRUE,但它从未发生。

最佳答案

UIViewController isEqual: 的实现只是检查内存地址。因此,两个单独的实例将不相等。

一种解决方案是检查对象的类型。

for (UIViewController *vc in newTabs){
if ([vc isKindOfClass:[SecondViewController class]]){
found=TRUE;
}
}

关于ios - 检查特定的 UIViewController 是否在 TabBarItems 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21501325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com