好吧,我试图将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
我新的堆栈溢出,很抱歉的乱码格式。
P.S我使用界面左上方的箭头返回到原始界面。我也使用Objective-C
在此先感谢。
我对此很陌生,并不真正了解他们的工作方式,请您指点我的教程或解释? – Rohan
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html – AncAinu
真棒,感谢一堆! – Rohan