2015-10-07 94 views
1

我正在使用下面的代碼來檢查視圖控制器。比較類是給出不正確的輸出

NSLog(@"addProductClicked 1===%@", self.class); 

NSLog(@"addProductClicked 2===%@", [CategoriesViewController class]); 

if ([self.class isKindOfClass:[CategoriesViewController class]]) { 

    NSLog(@"you go it right"); 

} else { 
    NSLog(@"you go it wrong"); 
} 

我得到的輸出如下。

addProductClicked 1===CategoriesViewController 
addProductClicked 2===CategoriesViewController 

you go it wrong 

任何想法是什麼問題?


只是爲了更新,下面就是我定義我的視圖控制器...

@interface CategoriesViewController : GlobalViewController { 

現在GlobalViewController我有方法,其中,我上面的檢查......

回答

1

您想要進行類檢查的變量應作爲對象傳入,而不是作爲類傳入。

if ([self isKindOfClass:[CategoriesViewController class]]) { 
    NSLog(@"you go it right"); 
} else { 
    NSLog(@"you go it wrong"); 
} 
1

那是錯誤的比較。您可以在該課程的對象上致電isKindOfClass:。就像這樣:

CategoriesViewController *obj = [[CategoriesViewController alloc] init]; 
[obj isKindOfClass:CategoriesViewController]; 

在你的情況下,你可能想要檢查self