How to check if a UIViewController is of a particular sub-class in objective c?
23,850
Solution 1
The isKindOfClass:
method indicates whether an object is an instance of given class or an instance of a subclass of that class.
if ([instance1 isKindOfClass:[CustomUIViewController class]]) {
// code
}
If you want to check whether an object is an instance of a given class (but not an instance of a subclass of that class), use isMemberOfClass:
instead.
Solution 2
var someVC: UIViewController
if someVC is MyCustomVC {
//code
}
Solution 3
Swift version:
var someVC: UIViewController
if someVC.isKindOfClass(MyCustomVC) {
//code
}
Solution 4
Try:
[vc isKindOfClass:[CustomViewController class]];
Solution 5
I just wanted to add in addition to this answer that if you're wanting to see if a view controller is of a certain type in a switch statement (in Swift) you can do it like this:
var someVC: UIViewController?
switch someVC {
case is ViewController01: break
case is ViewController02: break
case is ViewController03: break
default: break
}
Related videos on Youtube
Author by
Arcadian
Updated on July 09, 2022Comments
-
Arcadian almost 2 years
I want to be able to check the type of a UIViewController to see if it is of a certain type like this
c code
if (typeof(instance1) == customUIViewController) { customUIViewController test = (customViewController)instance1; // do more stuff }
-
Moshe over 13 yearsYou should be able to use C code in the iPhone SDK... Have you tried your code yet?
-
TOMKA over 13 yearsExcept that is not valid C code...
-
Moshe over 13 years@dreamlax - I never said it was.
-