好的,所以我试图将一个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的新手,对于凌乱的代码格式化感到抱歉。
P.S我使用界面左上角的箭头返回原始界面。我也在使用Objective-C
提前致谢。
答案 0 :(得分:0)
您需要在控制器中设置要更改的属性或方法(第一个控制器将更改),然后使用delegate
模式返回结果。
答案 1 :(得分:0)
你试图在Watch应用程序中执行此操作,是吗?我不知道代表是不行的,但当我为我的Watch应用程序执行此操作时,我使用了context
的{{1}}参数。
WKInterfaceController::presentControllerWithName:context:
是您要传递的值的NSDictionary。其中一个值可以是指向呈现控制器的指针。
因此,尝试破译您在应用中尝试的内容,我认为正确的做法是:
在 ORIGINAL WKInterfaceController:
中context
在 OTHER WKInterfaceController:
中- (IBAction)buttonThatOpensOtherIC
{
NSDictionary *context = @{
@"firstController" : self,
};
[self pushControllerWithName:@"Other IC"
context:context];
}
}
*请注意在 OTHER 接口控制器中使用- (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];
}
。
免责声明#1: 我没有执行此代码,因此可能会出现拼写错误。它适用于我使用的工作代码。
免责声明#2: 我没有为WatchOS 2更新我的应用程序。我怀疑这部分内容已经发生了变化,但这是有可能的。