I want to be able to check the type of a UIViewController to see if it is of a certain type like this
我想要检查UIViewController的类型看看它是否是这样的类型
c code
c代码
if (typeof(instance1) == customUIViewController)
{
customUIViewController test = (customViewController)instance1;
// do more stuff
}
6 个解决方案
#1
34
The isKindOfClass:
method indicates whether an object is an instance of given class or an instance of a subclass of that class.
isKindOfClass:方法指示对象是给定类的实例还是该类的子类的实例。
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.
如果您想检查对象是否是给定类的实例(但不是该类的子类的实例),那么使用isMemberOfClass:代替。
#2
13
var someVC: UIViewController
if someVC is MyCustomVC {
//code
}
#3
8
Swift version:
斯威夫特版本:
var someVC: UIViewController
if someVC.isKindOfClass(MyCustomVC) {
//code
}
#4
5
Try:
试一试:
[vc isKindOfClass:[CustomViewController class]];
#5
2
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:
我想补充的是,如果你想知道一个视图控制器在switch语句(Swift)中是否是特定类型的你可以这样做:
var someVC: UIViewController?
switch someVC {
case is ViewController01: break
case is ViewController02: break
case is ViewController03: break
default: break
}
#6
1
Swift 3.0 in latest, we have to add a self along with the class name or it will throw an error "Expected member name or constructor call after type name" the below code u can use for Swift 3 and above
在最新的Swift 3.0中,我们必须添加一个self和类名,否则它将抛出一个错误“预期的成员名或类型名后的构造函数调用”
for viewController in viewControllers {
if viewController.isKind(of: OurViewController.self){
print("yes it is OurViewController")
self.navigationController?.popToViewController(viewController, animated: true)
}
}
#1
34
The isKindOfClass:
method indicates whether an object is an instance of given class or an instance of a subclass of that class.
isKindOfClass:方法指示对象是给定类的实例还是该类的子类的实例。
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.
如果您想检查对象是否是给定类的实例(但不是该类的子类的实例),那么使用isMemberOfClass:代替。
#2
13
var someVC: UIViewController
if someVC is MyCustomVC {
//code
}
#3
8
Swift version:
斯威夫特版本:
var someVC: UIViewController
if someVC.isKindOfClass(MyCustomVC) {
//code
}
#4
5
Try:
试一试:
[vc isKindOfClass:[CustomViewController class]];
#5
2
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:
我想补充的是,如果你想知道一个视图控制器在switch语句(Swift)中是否是特定类型的你可以这样做:
var someVC: UIViewController?
switch someVC {
case is ViewController01: break
case is ViewController02: break
case is ViewController03: break
default: break
}
#6
1
Swift 3.0 in latest, we have to add a self along with the class name or it will throw an error "Expected member name or constructor call after type name" the below code u can use for Swift 3 and above
在最新的Swift 3.0中,我们必须添加一个self和类名,否则它将抛出一个错误“预期的成员名或类型名后的构造函数调用”
for viewController in viewControllers {
if viewController.isKind(of: OurViewController.self){
print("yes it is OurViewController")
self.navigationController?.popToViewController(viewController, animated: true)
}
}