isKindOfClass Bool if 语句记录 NO

isKindOfClass Bool if statement logging NO

我正在检查 toViewController(我的 tabBar 中的第二个选项卡)是否属于 class MatchCenterViewController,但 else 语句是 运行 相反,这告诉我它不是那个 class。

我确定该选项卡中的 UIViewController 已连接到 MatchCenterViewController,那么还有什么可能导致此 if 语句不起作用?

NSLog(@"numberOfMatches is 1");

UIViewController *toViewController = [self.tabBarController viewControllers][1];

NSLog(@"toViewController: %@", toViewController);

if ([toViewController isKindOfClass:[MatchCenterViewController class]]) {

    NSLog(@"2nd matchcenter if statement works");
    MatchCenterViewController *matchViewController = (MatchCenterViewController *)toViewController;

    matchViewController.didAddNewItem = YES;

    NSLog(@"alright they're set, time to switch");

}
else {
    NSLog(@"toViewController is not MatchCenterViewController");
}
[self.tabBarController setSelectedIndex:1];

您可以添加 NSLog(@"toViewController is of class: %@", NSStringFromClass([toViewController class]); 并查看实际视图控制器 class。

或者如果 didAddNewItem 是一个只有 MatchCenterViewController 有的 属性,你可以这样试试:

if ([toViewController respondsToSelector:@selector(setDidAddNewItem:)]) {
    // this is MatchCenterViewController
} else {
    // this is not MatchCenterViewController
}