JDev
JDev

Reputation: 124

reference interfacecontroller in swift, watchkit

How can I get the Instance (reference) to the InterfaceController in my ExtensionDelegate?

In my normal AppDelegate I do it like this:

let vc = window?.rootViewController as! ViewController?

but in the InterfaceController I don't know how to do it. I only have the standard Interface controller with the class Interfacecontroller(The one you have when you create a new project).

Or if that doesn't work:

How can I call a pushnotification and add an observer like NSNotificationCenter.defaultCenter().addObserver(...) in the ExtensionDelegate and the InterfaceController?

EDIT: what i want to achieve is i want to set a variable from the ExtensionDelegate.swift in the InterfaceController.swift.

Thanks in Advance!!

Upvotes: 0

Views: 449

Answers (2)

Aaron Brager
Aaron Brager

Reputation: 66302

You can access the WKExtension instance's rootInterfaceController property:

if let controller = WKExtension.sharedExtension().rootInterfaceController {
    // do something with controller
}

If you subclassed the interface controller, you'll need to explicitly cast it:

if let controller = WKExtension.sharedExtension().rootInterfaceController as? MyInterfaceControllerSubclass {
    // do something with controller
}

Upvotes: 1

Jeff Kelley
Jeff Kelley

Reputation: 19071

You can use the rootInterfaceController property on WKExtension:

WKExtension.sharedExtension().rootInterfaceController

Upvotes: 1

Related Questions