isKindOfClass bool if语句记录NO

时间:2015-07-04 19:45:48

标签: ios objective-c uiviewcontroller

我正在检查我的tabBar中的第二个标签toViewController是否属于MatchCenterViewController类,但是else语句正在运行,这告诉我它不属于那个班级。

我很肯定该标签中的UIViewControllerMatchCenterViewController相关联,那么还有什么可能导致此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];

1 个答案:

答案 0 :(得分:1)

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

如果didAddNewItem是只有 MatchCenterViewController 的属性,您可以尝试这种方式:

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