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

标签 ios swift appdelegate

我在 class AppDelegate 内的 AppDelegate.swift 文件中有一个变量 var window: UIWindow? 我想在 内的其他类 xyz.swift 文件中使用它类 xyz 在这里解释 Get current view controller from the app delegate (modal is possible)但我在第一行遇到错误任何帮助将不胜感激。这是来自 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!")
          }
      }
  }

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

最佳答案

您可能必须执行以下操作:

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

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

关于ios - 如何在 AppDelegate 中定义的其他类中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30180766/

相关文章:

swift - 排序时间字符串 (HH :mm) in ascending order in Swift

swift - 如何使用 AVFoundation 制作视频填充框

ios - 在 API 中创建 if 条件时,如何修复 swift 代码中调用 Storyboard 的速度?

ios - 如何获取带有命名空间(或框架名称)的字符串形式的 Swift 类型名称

ios - 将大量数据存储到核心数据中

iphone - 在调用另一个方法时阻止 AppDelegate 加载 ViewController

swift - 从Navigation改为TabBar,CoreData不加载

ios - 从 appdelegate 设置 UIViewController 委托(delegate)

android - 将 Unity 项目连续导出到 Android Studio/XCode

ios - 如何将 NSDictionary 传递给函数并在 Swift 2 中返回另一个 NSDictionary