我在 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/