【发布时间】:2017-02-15 00:14:23
【问题描述】:
一般情况下是否可以从其他接口控制器调用扩展委托中的方法?
类似:
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 中定义的方法。
【问题讨论】:
-
您想从另一个控制器调用委托中实现的自定义函数?
-
好吧,我的 InterfaceController 中有一个方法,例如 -(void)doSomething。我想从我的 ExtensionDelegate 类中调用这个方法。
-
能分享一下ExtensionDelegate类和interfaceController的代码吗...
-
我当然添加了 - 见上文
-
啊是的..创建你的类的实例,然后调用你想要的方法或使用静态类,这将允许你在不创建类的实例的情况下调用方法..