如何在AppDelegate中定义的其他类中使用变量

时间:2022-08-24 14:52:43

I have a variable var window: UIWindow? in AppDelegate.swift file inside class AppDelegate that I want to use in other class xyz.swift file inside class xyz as explained in here Get current view controller from the app delegate (modal is possible) but I am getting error at the first line any help will be appreciated. here is the code from xyz.swift

我有一个变量var窗口:UIWindow?在类AppDelegate中的AppDelegate.swift文件中,我想在类xyz中的其他类xyz.swift文件中使用,如此处所述从app delegate获取当前视图控制器(模态是可能的)但是我在第一行收到任何错误帮助将不胜感激。这是来自xyz.swift的代码

func CurrentView() -> UIView
  {
     let navigationController = window?.rootViewController as? UINavigationController // Use of Unresolved identifier 'window'
     if let activeController = navigationController!.visibleViewController {
     if activeController.isKindOfClass( MyViewController )  {
       println("I have found my controller!")
          }
      }
  }

Even if I use let navigationController = AppDelegate.window?.rootViewController as? UINavigationController error is 'AppDelegate.Type' does not have member named 'window'

即使我使用let navigationController = AppDelegate.window?.rootViewController作为? UINavigationController错误是'AppDelegate.Type'没有名为'window'的成员

2 个解决方案

#1


You may have to do as follows:

您可能需要执行以下操作:

    var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let navigationController = appDelegate.window?....

As @Dave Durbin has pointed out you are trying to use a variable defined in one class into another class without the reference of the defining class.

正如@Dave Durbin所指出的那样,你试图将一个类中定义的变量用于另一个类而不引用定义类。

#2


This line of code is in xyz.swift

这行代码在xyz.swift中

  let navigationController = window?.rootViewController as? UINavigationController // Use of Unresolved identifier 'window'

You don't provide any context for window so it's expected to be in this class or a global variable.

您没有为窗口提供任何上下文,因此它应该在此类或全局变量中。

This is closer:

这更接近:

navigationController = AppDelegate.window?.rootViewController as? UINavigationController

and you seem to realise that you need to reference the window variable within your AppDelegate instance however the syntax you are using references a static variable and window is a member variable.

并且您似乎意识到您需要在AppDelegate实例中引用窗口变量,但是您使用的语法引用静态变量,窗口​​是成员变量。

I suggest you read through the swift manual and gain a better understanding of variable scopes and check this:

我建议你阅读swift手册并更好地理解变量范围并检查:

How do I get a reference to the app delegate in Swift?

如何在Swift中获得对应用程序委托的引用?

#1


You may have to do as follows:

您可能需要执行以下操作:

    var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let navigationController = appDelegate.window?....

As @Dave Durbin has pointed out you are trying to use a variable defined in one class into another class without the reference of the defining class.

正如@Dave Durbin所指出的那样,你试图将一个类中定义的变量用于另一个类而不引用定义类。

#2


This line of code is in xyz.swift

这行代码在xyz.swift中

  let navigationController = window?.rootViewController as? UINavigationController // Use of Unresolved identifier 'window'

You don't provide any context for window so it's expected to be in this class or a global variable.

您没有为窗口提供任何上下文,因此它应该在此类或全局变量中。

This is closer:

这更接近:

navigationController = AppDelegate.window?.rootViewController as? UINavigationController

and you seem to realise that you need to reference the window variable within your AppDelegate instance however the syntax you are using references a static variable and window is a member variable.

并且您似乎意识到您需要在AppDelegate实例中引用窗口变量,但是您使用的语法引用静态变量,窗口​​是成员变量。

I suggest you read through the swift manual and gain a better understanding of variable scopes and check this:

我建议你阅读swift手册并更好地理解变量范围并检查:

How do I get a reference to the app delegate in Swift?

如何在Swift中获得对应用程序委托的引用?