据我所知,这可以在Objective-C中使用:
self.window.rootViewController.class == myViewController
如何检查当前视图控制器是否是特定的视图控制器?
答案 0 :(得分:68)
要检查Swift中的类,请使用"is" (as explained under "checking Type" in the chapter called Type Casting in the Swift Programming Guide)
if self.window.rootViewController is MyViewController {
//do something if it's an instance of that class
}
答案 1 :(得分:38)
针对swift3编译器进行了更新,使其更加贴合!和?
if let wd = UIApplication.shared.delegate?.window {
var vc = wd!.rootViewController
if(vc is UINavigationController){
vc = (vc as! UINavigationController).visibleViewController
}
if(vc is LogInViewController){
//your code
}
}
答案 2 :(得分:18)
如果使用导航控制器,则可以轻松迭代视图控制器。然后您可以检查特定实例:
if let viewControllers = navigationController?.viewControllers {
for viewController in viewControllers {
// some process
if viewController.isKindOfClass(MenuViewController) {
println("yes it is")
}
}
}
答案 3 :(得分:10)
我必须在AppDelegate中找到当前的viewController。 我用过这个
//at top of class
var window:UIWindow?
// inside my method/function
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
println("Found it!!!")
}
}
}
答案 4 :(得分:7)
试试这个
if self is MyViewController {
}
答案 5 :(得分:7)
要想了解Thapa的答案,你需要在使用之前转换到viewcontroller类......
if let wd = self.view.window { var vc = wd.rootViewController! if(vc is UINavigationController){ vc = (vc as! UINavigationController).visibleViewController } if(vc is customViewController){ var viewController : customViewController = vc as! customViewController
答案 6 :(得分:6)
Swift 3
不确定你们,但我很难用这个。我做了这样的事情:
if let window = UIApplication.shared.delegate?.window {
if var viewController = window?.rootViewController {
// handle navigation controllers
if(viewController is UINavigationController){
viewController = (viewController as! UINavigationController).visibleViewController!
}
print(viewController)
}
}
我一直在获取应用的初始视图控制器。出于某种原因,它想要保留根视图控制器,无论如何。所以我只创建了一个全局字符串类型变量currentViewController
并在每个viewDidLoad()
中自己设置它的值。我只需要知道我在哪个屏幕上&这对我很有用。
答案 7 :(得分:6)
Swift 4,Swift 5
req.Header["User-Agent"]
答案 8 :(得分:1)
对于类型,您可以使用is
,如果它是您自己的viewcontroller类,那么您需要使用isKindOfClass
,如:
let vcOnTop = self.embeddedNav.viewControllers[self.embeddedNav.viewControllers.count-1]
if vcOnTop.isKindOfClass(VcShowDirections){
return
}
答案 9 :(得分:1)
斯威夫特3 |检查视图控制器是否是其自身的根。
您可以在视图控制器中访问window
,只需使用self.view.window
。
上下文:我需要更新视图的位置并在旋转设备时触发动画。如果视图控制器处于活动状态,我只想这样做。
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(deviceDidRotate),
name: .UIApplicationDidChangeStatusBarOrientation,
object: nil
)
}
func deviceDidRotate() {
guard let window = self.view.window else { return }
// check if self is root view controller
if window.rootViewController == self {
print("vc is self")
}
// check if root view controller is instance of MyViewController
if window.rootViewController is MyViewController {
print("vc is MyViewController")
}
}
}
如果在MyViewController处于活动状态时旋转设备,您将看到上面的行打印到控制台。如果MyViewController未激活,您将看不到它们。
如果您对我使用UIDeviceOrientationDidChange
代替.UIDeviceOrientationDidChange
的原因感到好奇,请查看this answer。
答案 10 :(得分:1)
SELECT *
FROM ORGANIZATION_TABLE
START WITH PARENT_ID = 6
CONNECT BY PRIOR CHILD_ID = PARENT_ID;
答案 11 :(得分:1)
检查对我来说更有效的方法What is .self
if ((self.window.rootViewController?.isKind(of: WebViewController.self))!)
{
//code
}
答案 12 :(得分:0)
if let index = self.navigationController?.viewControllers.index(where: { $0 is MyViewController }) {
let vc = self.navigationController?.viewControllers[vcIndex] as! MyViewController
self.navigationController?.popToViewController(vc, animated: true)
} else {
self.navigationController?.popToRootViewController(animated: true)
}
答案 13 :(得分:0)
var top = window?.rootViewController
while ((top?.presentedViewController) != nil) {
top = top?.presentedViewController
}
if !(type(of: top!) === CallingVC.self) {
top?.performSegue(withIdentifier: "CallingVC", sender: call)
}