2017-02-14 1 views
0

일반적으로 Extension 대리자의 메서드를 다른 IntefaceController에서 호출 할 수 있습니까?Extension 대리인의 메소드 호출

뭔가 같은 :

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 

ExtensionDelegate :

#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에 정의 된 메소드를 호출합니다.

+0

다른 컨트롤러에서 대리인에 구현 된 사용자 지정 함수를 호출 하시겠습니까? –

+0

글쎄, 내 InterfaceController에 - (void) doSomething과 같은 메소드가있다. ExtensionDelegate 클래스에서이 메서드를 호출하려고합니다. –

+0

당신은 ExtensionDelegate 클래스와 interfaceController 코드를 공유 할 수 있습니까? –

답변

1

ExtensionDelegate에서 WKInterfaceController을 초기화하고 메소드를 호출 할 수있는 방법이 없습니다. 메서드를 호출하려는 컨트롤러가 루트 컨트롤러 인 경우 ExtensionDelegateWKExtension에서 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() 
} 

*이 내 목표 - C는 약간 녹슨입니다.

코드 예제에서 로컬 알림을 기반으로 작업을 수행하려고하므로 인터페이스 컨트롤러 자체에서 알림을 처리하는 것이 가장 좋습니다. 당신이

watchOS 3

handleAction(withIdentifier:for:) reference

watchOS 2

를 사용하는 watchOS 내용에 따라 handleAction(withIdentifier:for:) reference

여기에 이러한 방법에 대한

중요 사항은 handleAction를 구현하지 않는 확장 위임 호출 할 (withIdentifier : for :) 메소드에서 WatchKit은 앱의 루트 인터페이스 컨트롤러에서이 메소드를 호출하여 알림 인터페이스의 버튼 탭에 응답합니다.

+0

그게 내가 원하는 것. iOS에서는 AppDelegate에서 메서드를 호출해도 문제가 없으므로 혼란 스러웠습니다. 어쨌든, 그래서 나는 handleAction을 ViewController로 대체했습니다. 감사! –

+0

@ASP_Soldier 문제 없음! 내 대답이 귀하의 질문에 대답했다고 생각되면 동의 한 것으로 표시하십시오. 고맙습니다. – lostAtSeaJoshua