好吧,我試圖將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