How to check if a UIViewController is a specific subclass in a c object?

I want to check the type of the UIViewController to see if it has a specific type like this

c code

if (typeof(instance1) == customUIViewController) { customUIViewController test = (customViewController)instance1; // do more stuff } 
+17
source share
6 answers

The isKindOfClass: method indicates whether the object is an instance of this class or an instance of a subclass of this class.

 if ([instance1 isKindOfClass:[CustomUIViewController class]]) { // code } 

If you want to check if the object is an instance of this class (but not an instance of a subclass of this class), use isMemberOfClass: instead.

+32
source
 var someVC: UIViewController if someVC is MyCustomVC { //code } 
+13
source

Quick version:

 var someVC: UIViewController if someVC.isKindOfClass(MyCustomVC) { //code } 
+8
source

Try:

 [vc isKindOfClass:[CustomViewController class]]; 
+5
source

I just wanted to add in addition to this answer that if you want to see if a view controller has a certain type in an operator switch (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 } 
+2
source

Swift 3.0 in the latter case, we must add "I" along with the class name or it will throw an error " Expected member name or constructor call after type name " below u code can be used for Swift 3 and above

  for viewController in viewControllers { if viewController.isKind(of: OurViewController.self){ print("yes it is OurViewController") self.navigationController?.popToViewController(viewController, animated: true) } } 
0
source

All Articles