扩展委托中的调用方法
Call Methods in Extension Delegate
一般情况下是否可以从其他 IntefaceController 调用扩展委托中的方法?
类似于:
InterfaceController *interfaceController =[[InterfaceController alloc] init];
interfaceController callMethod
我的界面控制器
#import "InterfaceController.h"
#import "OrdinaryEventRow.h"
#import <UIKit/UIKit.h>
#import <WatchConnectivity/WatchConnectivity.h>
@interface InterfaceController()
@implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
//Configure interface objects here.
-(void)doSomething {
[self presentControllerWithName:@"goalView" context:nil];
}
@end
扩展委托:
#import "ExtensionDelegate.h"
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
#import "setGoal.h"
@implementation ExtensionDelegate
//Handle Local Notification Actions
-(void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UNNotification *)localNotification{
if([identifier isEqualToString:@"action"]){
//Setup WCSession
if ([WCSession isSupported]) {
[[WCSession defaultSession] setDelegate:self];
[[WCSession defaultSession] activateSession];
//Get the value from slider
NSString *someString = [[NSUserDefaults standardUserDefaults]
stringForKey:@"Update"];
NSString *Update = @"Update";
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[Update] forKeys:@[@"Update"]];
//Send Message to the iPhone (handle over the goal value)
[[WCSession defaultSession] sendMessage:applicationData
replyHandler:^(NSDictionary *reply) {
//handle reply from iPhone app here
}
errorHandler:^(NSError *error) {
//catch any errors here
}
];
}
}
//If Goal Setting was clicked
if([identifier isEqualToString:@"action3"]){
//here I want to call doSomething from InterfaceController
}
}
所以我只想从 ExtensionDelegate 调用 InterfaceController 中定义的方法。
无法从 ExtensionDelegate
初始化 WKInterfaceController
并在其上调用方法。如果您尝试调用方法的控制器是根控制器,您可以从 ExtensionDelegate
中的 WKExtension
获取 rootController
强制转换它并在其上调用方法。
// Objective-C
if ([WKExtension shared].rootController isKindOfClass:[InitialInterfaceController class]) {
(InitialInterfaceController *)[WKExtension shared].rootController customMethod];
}
// Swift
if let root = WKExtension.shared().rootInterfaceController as? InitialInterfaceController {
root.customMethod()
}
* 我的 objective-c 有点生疏,所以如果那里有语法错误,请更新或在评论中告诉我,我可以编辑。
根据您的代码示例,您正在尝试根据本地通知执行操作,因此最好的办法是在界面控制器本身中处理通知。取决于您使用的 watchOS
watchOS 3
handleAction(withIdentifier:for:) reference
watchOS 2
handleAction(withIdentifier:for:) reference
这里要注意的是要调用这些方法你的扩展委托没有实现 handleAction(withIdentifier:for:) 方法,WatchKit 在你的应用程序的根接口控制器上调用这个方法来响应你的通知接口中的按钮点击。
一般情况下是否可以从其他 IntefaceController 调用扩展委托中的方法?
类似于:
InterfaceController *interfaceController =[[InterfaceController alloc] init];
interfaceController callMethod
我的界面控制器
#import "InterfaceController.h"
#import "OrdinaryEventRow.h"
#import <UIKit/UIKit.h>
#import <WatchConnectivity/WatchConnectivity.h>
@interface InterfaceController()
@implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
//Configure interface objects here.
-(void)doSomething {
[self presentControllerWithName:@"goalView" context:nil];
}
@end
扩展委托:
#import "ExtensionDelegate.h"
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
#import "setGoal.h"
@implementation ExtensionDelegate
//Handle Local Notification Actions
-(void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UNNotification *)localNotification{
if([identifier isEqualToString:@"action"]){
//Setup WCSession
if ([WCSession isSupported]) {
[[WCSession defaultSession] setDelegate:self];
[[WCSession defaultSession] activateSession];
//Get the value from slider
NSString *someString = [[NSUserDefaults standardUserDefaults]
stringForKey:@"Update"];
NSString *Update = @"Update";
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[Update] forKeys:@[@"Update"]];
//Send Message to the iPhone (handle over the goal value)
[[WCSession defaultSession] sendMessage:applicationData
replyHandler:^(NSDictionary *reply) {
//handle reply from iPhone app here
}
errorHandler:^(NSError *error) {
//catch any errors here
}
];
}
}
//If Goal Setting was clicked
if([identifier isEqualToString:@"action3"]){
//here I want to call doSomething from InterfaceController
}
}
所以我只想从 ExtensionDelegate 调用 InterfaceController 中定义的方法。
无法从 ExtensionDelegate
初始化 WKInterfaceController
并在其上调用方法。如果您尝试调用方法的控制器是根控制器,您可以从 ExtensionDelegate
中的 WKExtension
获取 rootController
强制转换它并在其上调用方法。
// Objective-C
if ([WKExtension shared].rootController isKindOfClass:[InitialInterfaceController class]) {
(InitialInterfaceController *)[WKExtension shared].rootController customMethod];
}
// Swift
if let root = WKExtension.shared().rootInterfaceController as? InitialInterfaceController {
root.customMethod()
}
* 我的 objective-c 有点生疏,所以如果那里有语法错误,请更新或在评论中告诉我,我可以编辑。
根据您的代码示例,您正在尝试根据本地通知执行操作,因此最好的办法是在界面控制器本身中处理通知。取决于您使用的 watchOS
watchOS 3
handleAction(withIdentifier:for:) reference
watchOS 2
handleAction(withIdentifier:for:) reference
这里要注意的是要调用这些方法你的扩展委托没有实现 handleAction(withIdentifier:for:) 方法,WatchKit 在你的应用程序的根接口控制器上调用这个方法来响应你的通知接口中的按钮点击。