Ios 检查选项卡项中是否有特定的UIViewController
如果用户登录或未登录,我尝试使用UITabBarController以编程方式添加或删除某些ViewController作为选项卡 我使用以下代码添加ViewController“SecondViewController”:Ios 检查选项卡项中是否有特定的UIViewController,ios,uitabbarcontroller,Ios,Uitabbarcontroller,如果用户登录或未登录,我尝试使用UITabBarController以编程方式添加或删除某些ViewController作为选项卡 我使用以下代码添加ViewController“SecondViewController”: [newTabs addObject:second]; [self.tabBarController setViewControllers:newTabs]; 并且,我运行下面的代码来检查特定的ViewController(第二个)是否在数组中。但它不起作用:vs永远不等
[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>
2014-02-01 18:38:18.755应用程序登录测试[1469:11303]PostData:
2014-02-01 18:38:18.756应用程序登录测试[1469:11303]PostData:
(这是我运行vc和second的NSLog的时候,比较两者,并理解它们为什么不相等。)
我已经找了一段时间了,但是我找不到解释!found应该设置为TRUE,但它永远不会发生。下面一行
[情节提要实例化eviewController标识符:@“SecondViewController”];
将生成SecondViewController的新实例
也根据苹果文件确认
“每次调用指定视图控制器时,此方法都会创建该视图控制器的新实例。”
因此,您对isEqual的比较不一定会起作用。相反,您可以这样做:
...
if([vc是类的种类:[第二类]]){
一种可能更好的方法是存储对SecondViewController的引用,而不是每次都实例化一个新的SecondViewController。然后,您可以使用isEqual:检查视图控制器是否已经在UITableController中,如果它不存在,则添加您已经拥有的实例。
UIViewController isEqual的初始化:
只是检查内存地址。因此,两个单独的实例将不相等
一种解决方案是检查对象的类型
for (UIViewController *vc in newTabs){
if ([vc isKindOfClass:[SecondViewController class]]){
found=TRUE;
}
}