Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a UIViewController is of a particular sub-class in objective c?

Tags:

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
}
like image 741
Arcadian Avatar asked Nov 06 '10 23:11

Arcadian


4 Answers

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.

like image 137
James Huddleston Avatar answered Oct 16 '22 12:10

James Huddleston


var someVC: UIViewController

if someVC is MyCustomVC {
    //code
}
like image 31
Harry Ng Avatar answered Oct 16 '22 11:10

Harry Ng


Swift version:

var someVC: UIViewController

if someVC.isKindOfClass(MyCustomVC) {
    //code
}
like image 25
Esqarrouth Avatar answered Oct 16 '22 11:10

Esqarrouth


Try:

[vc isKindOfClass:[CustomViewController class]];
like image 41
Kevin Sylvestre Avatar answered Oct 16 '22 11:10

Kevin Sylvestre