【问题标题】:Call Methods in Extension Delegate扩展委托中的调用方法
【发布时间】: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的代码吗...
  • 我当然添加了 - 见上文
  • 啊是的..创建你的类的实例,然后调用你想要的方法或使用静态类,这将允许你在不创建类的实例的情况下调用方法..

标签: ios xcode watchkit


【解决方案1】:

没有办法从ExtensionDelegate 初始化WKInterfaceController 并在其上调用方法。如果您尝试调用方法的控制器是根控制器,您可以从WKExtension 中的ExtensionDelegate 中获取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 有点生疏,所以如果那里有语法错误,请更新或在 cmets 中告诉我,我可以编辑。

在您的代码示例中,您正尝试根据本地通知执行操作,因此最好的办法是在接口控制器本身中处理通知。取决于您使用的 watchOS

watchOS 3

handleAction(withIdentifier:for:) reference

watchOS 2

handleAction(withIdentifier:for:) reference

这里需要注意的是,要调用这些方法,您的扩展委托没有实现 handleAction(withIdentifier:for:) 方法,WatchKit 在您应用的根界面控制器上调用此方法来响应通知界面中的按钮点击。

【讨论】:

  • 好吧,我就是这么想的。我很困惑,因为在 iOS 中调用 AppDelegate 中的方法没有问题。无论如何,所以我将handleAction替换为ViewController。谢谢!
  • @ASP_Soldier 没问题!如果您认为我的回答已经回答了您的问题,请将其标记为已接受。谢谢。
  • 如果它不是根控制器,我们如何调用方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 2015-03-22
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多