1

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

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'

Community
  • 1
  • 1
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • Where have you defined var windows? Is it in AppDelegate class or just inside the file? Also where is CurrentView() method is defined? Please post code with context. – Abdullah May 12 '15 at 02:41
  • `var window: UIWindow?` is in AppDelegate class and `CurrentView()` function is another class named `class ServiceManager: NSObject {` what else code you want ? – Varun Naharia May 12 '15 at 02:46

2 Answers2

1

This line of code is in 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.

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

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

Community
  • 1
  • 1
Dave Durbin
  • 3,562
  • 23
  • 33
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.

Abdullah
  • 7,143
  • 6
  • 25
  • 41