首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apple Watch - Watch Kit -未被调用的委托方法

Apple Watch - Watch Kit -未被调用的委托方法
EN

Stack Overflow用户
提问于 2015-10-19 05:24:42
回答 2查看 750关注 0票数 0

好的,所以我试图将一个int传递给另一个接口,编辑int并将它返回到原来的接口。我正在尝试使用委托来实现这一点,并且我相信我已经正确地设置了它,并且它似乎没有在它应该调用的时候被调用。

代码语言:javascript
复制
//
//  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
代码语言:javascript
复制
//  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
代码语言:javascript
复制
//
//  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
代码语言:javascript
复制
//
//  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

我是新的堆栈溢出,抱歉,混乱的代码格式。

使用界面左上角的箭头返回原始接口。也在使用目标C

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-10-19 06:19:18

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

票数 0
EN

Stack Overflow用户

发布于 2015-10-20 17:35:11

你想在一个手表应用程序里做这个,是吗?我不知道委托不起作用,但当我为我的Watch应用程序这样做时,我使用了context参数WKInterfaceController::presentControllerWithName:context:

context是要传递的值的NSDictionary。其中一个值可以是指向呈现控制器的指针。

所以,试图破解你在你的应用程序中尝试的东西,我相信正确的做法是:

原始WKInterfaceController中:

代码语言:javascript
复制
- (IBAction)buttonThatOpensOtherIC
{
    NSDictionary *context = @{
                              @"firstController" : self,
                              };

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

OTHER WKInterfaceController中:

代码语言:javascript
复制
- (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];
}

*注意awakeWithContext:其他接口控制器中的使用。

免责声明#1:我还没有执行这段代码,所以可能会有一个错误。这是我使用的工作代码的一种改编。

免责声明# 2 :我还没有为WatchOS 2更新我的应用程序。我怀疑这部分事情改变了,但这是可能的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33207172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档