[英]Apple Watch - Watch Kit - Delegate method not being called
好的,所以我尝试将一个int传递给另一个接口,编辑该int并将其返回给原始接口。 我正在尝试使用委托来实现此目的,并且我相信我已经正确设置了它,并且该方法似乎没有被调用。
//
// 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
我是Stack Overflow的新手,很抱歉混乱的代码格式。
PS我使用界面左上角的箭头返回到原始界面。 也正在使用Objective-C
提前致谢。
您需要设置一个属性或方法来在控制器中进行更改(第一个控制器将更改),然后使用delegate
模式返回结果。
您正在尝试在Watch应用中执行此操作,是吗? 我不知道委托不起作用,但是当我为Watch应用执行此操作时,我使用了WKInterfaceController::presentControllerWithName:context:
的context
参数。
context
是您要传递的值的NSDictionary。 这些值之一可以是指向呈现控制器的指针。
因此,尝试破译您在应用程序中尝试的操作时,我相信正确的做法是:
在原始 WKInterfaceController中:
- (IBAction)buttonThatOpensOtherIC
{
NSDictionary *context = @{
@"firstController" : self,
};
[self pushControllerWithName:@"Other IC"
context:context];
}
}
在其他 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];
}
*请注意在OTHER接口控制器中使用awakeWithContext:
免责声明#1:我尚未执行此代码,因此其中可能有错字。 这是我使用的工作代码的改编版。
免责声明#2:我尚未为WatchOS 2更新我的应用程序。我怀疑这部分内容已更改,但有可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.