在 WatchKit InterfaceController 中获取扩展委托的实例

Getting instance of Extension Delegate in WatchKit InterfaceController

我试图在 Apple Watch 应用程序的 InterfaceController 中获取 ExtensionDelegate 的实例,但似乎找不到方法。我在 Extension Delegate 中创建了一个我需要调用的方法,为此我需要一个 Extension Delegate 的共享实例。有办法吗?

与 iOS 应用一样,我们称其为:

[(AppDelegate *)[[UIApplication sharedApplication] delegate] methodName];

您可以使用以下行引用 ExtensionDelegate。请务必导入 WatchKit。

#import <WatchKit/WatchKit.h>

ExtensionDelegate* myDelegate = (ExtensionDelegate*)[[WKExtension sharedExtension] delegate];
[myDelegate methodName];

使用下面的代码得到相同的

ExtensionDelegate* myDelegate = (ExtensionDelegate*)[[WKExtension sharedExtension] 委托];