简体   繁体   中英

Apple Watch - Watch Kit - Delegate method not being called

Ok so I am attempting to pass an int to another interface, edit the int and give it back to the original interface. I am trying to use a delegate to achieve this and I believe I have it setup correctly and it appears the method is not being called when its supposed to.

//
//  InterfaceController.h
//  DelegateTest WatchKit Extension
//
//  Created by Rohan Hodge on 20/10/2015.
//  Copyright © 2015 Hodge Development. All rights reserved.
//

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "SecondController.h"

@interface InterfaceController : WKInterfaceController <DelegateTest>
{
    NSTimer *Update;
}
@property (strong, nonatomic) IBOutlet WKInterfaceLabel      *FirstControllerLabel;
@property (nonatomic,assign) int FirstInteger;
@property (nonatomic,assign) int RecievedInteger;
@property (nonatomic,assign) NSString *PassString;


@end

//  InterfaceController.m
//  DelegateTest WatchKit Extension
//
//  Created by Rohan Hodge on 20/10/2015.
//  Copyright © 2015 Hodge Development. All rights reserved.
//

#import "InterfaceController.h"


@interface InterfaceController()

@end


@implementation InterfaceController
@synthesize FirstInteger;
@synthesize RecievedInteger;
@synthesize PassString;

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Configure interface objects here.
}
-(void)UpdateVoid
{
     self.FirstControllerLabel.text = [NSString stringWithFormat:@"%i", FirstInteger];
}

- (void)willActivate {
    SecondController *interfaceController;
    interfaceController.delegate = self;
    Update = [NSTimer timerWithTimeInterval:1.0 target:self      selector:@selector(UpdateVoid) userInfo:nil repeats:YES];

    // This method is called when watch view controller is about to be visible to user
[super willActivate];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer   visible
    [super didDeactivate];
}

-(void)DelegateMethod:(int)ReturningInt
{
    [self popController];
    FirstInteger = ReturningInt;
    self.FirstControllerLabel.text = [NSString stringWithFormat:@"%i", FirstInteger];
}
- (IBAction)UpButton {
    FirstInteger++;
     self.FirstControllerLabel.text = [NSString stringWithFormat:@"%i", FirstInteger];
}

- (IBAction)DownButton {
    FirstInteger--;
    self.FirstControllerLabel.text = [NSString stringWithFormat:@"%i", FirstInteger];
}
- (IBAction)PassDataButton {
    PassString = [NSString stringWithFormat:@"%i", FirstInteger];
    [self pushControllerWithName:@"SecondController" context:PassString];
}

@end

//
//  SecondController.h
//  DelegateTest
//
//  Created by Rohan Hodge on 20/10/2015.
//  Copyright © 2015 Hodge Development. All rights reserved.
//

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

//This declaration of delegate.
@protocol DelegateTest <NSObject>

-(void) DelegateMethod:(int)ReturningInt;

@end

@interface SecondController : WKInterfaceController
{
    id delegate;

}
@property (nonatomic, assign) id <DelegateTest> delegate;
@property (strong, nonatomic) IBOutlet WKInterfaceLabel *SecondLabel;
@property (nonatomic,assign) NSString *RecievedString;
@property (nonatomic, assign) int FirstReceivedInteger;

@end

//
//  SecondController.m
//  DelegateTest
//
//  Created by Rohan Hodge on 20/10/2015.
//  Copyright © 2015 Hodge Development. All rights reserved.
//

#import "SecondController.h"
#import "InterfaceController.h"

@interface SecondController ()

@end

@implementation SecondController
@synthesize SecondLabel;
@synthesize FirstReceivedInteger;
@synthesize RecievedString;
@synthesize delegate = _delegate;

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    //This is where I receive the int inside of a string and split it from the string so I can change it
   RecievedString = context;
   FirstReceivedInteger = [RecievedString intValue];


    // Configure interface objects here.
}

- (void)willActivate {
    self.SecondLabel.text = [NSString stringWithFormat:@"%i",FirstReceivedInteger];

    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
}


- (IBAction)UpButton {
    FirstReceivedInteger++;
    self.SecondLabel.text = [NSString stringWithFormat:@"%i",FirstReceivedInteger];
}
- (IBAction)DownButton {
    FirstReceivedInteger--;
    self.SecondLabel.text = [NSString stringWithFormat:@"%i",FirstReceivedInteger];
}
  //This is a button that is ment to pass back the int.
  - (IBAction)ReturnToOriginalInterface:(id)sender{

      [self.delegate DelegateMethod:FirstReceivedInteger];

 }

 - (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
 }
  @end

I'm new to Stack Overflow, Sorry about the messy code formatting.

PS I use the Arrow in the top left of the interface to return to the original Interface. Also am using Objective-C

Thanks in advance.

您需要设置一个属性或方法来在控制器中进行更改(第一个控制器将更改),然后使用delegate模式返回结果。

You're attempting to do this in a Watch app, yes? I don't know that delegates don't work, but when I did this for my Watch app, I used the context parameter of WKInterfaceController::presentControllerWithName:context: .

context is a NSDictionary of the values you want to pass around. One of those values could be a pointer to the presenting controller.

So, trying to decipher what you're attempting in your app, I believe the proper thing to do is:

In the ORIGINAL WKInterfaceController:

- (IBAction)buttonThatOpensOtherIC
{
    NSDictionary *context = @{
                              @"firstController" : self,
                              };

    [self pushControllerWithName:@"Other IC"
                         context:context];
    }
}

In the OTHER WKInterfaceController:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    if (context)
    {
        self.originalInterfaceController = context[@"firstController"];
    }
}

//This is the button that calls the delegate method.
- (IBAction)ReturnToOriginalInterface:(id)sender
{
    // [self.delegate DelegateMethod:FirstReceivedInteger];

    if (self.originalInterfaceController) {
        self.originalInterfaceController.firstInteger = self.returningInt;
    }

    [self popController];
}

*Note the use of awakeWithContext: in the OTHER interface controller.

DISCLAIMER #1: I haven't executed this code, so there may be a typo in it. It is an adaptation for you of working code I do use.

DISCLAIMER #2: I haven't updated my app for WatchOS 2. I doubt this part of things changed, but it is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM