2015-10-19 49 views
0

好吧,我试图将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

在此先感谢。

回答

0

您需要设置一个属性或方法在您的控制器中进行更改(即您的第一个控制器将更改),然后以delegate模式返回结果。

+0

我对此很陌生,并不真正了解他们的工作方式,请您指点我的教程或解释? – Rohan

+0

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html – AncAinu

+0

真棒,感谢一堆! – Rohan

0

您试图在Watch应用程序中执行此操作,是的?我不知道代表不工作,但是当我为Watch应用程序执行此操作时,我使用WKInterfaceController::presentControllerWithName:context:的参数context

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

所以,试图破译你试图在你的应用是什么,我相信做正确的事情是:

ORIGINAL 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]; 
} 

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

免责声明#1:我还没有执行该代码,因此有可能是一个错字在里面。这是适合你使用的工作代码。

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

+0

谢谢你今晚会玩,只有一个问题是什么是originalInterfacecontroller?这是从哪里来的? – Rohan

+0

在你原来的文章中,你说过,“我试图将int传递给另一个接口,编辑int并将其返回到原始接口。” 'originalInterfaceController'应该是将int传递给另一个IC然后再接收它的那个。在Watch应用程序中,每个界面都由一个单独的WKInterfaceController进行管理。两个有两个接口,你必须有两个控制器。这有意义吗?我误解了你正在尝试的一些事情吗? –

+0

啊,这是我的错,在原来的接口控制器中,我使用pushControllerWithName传递它,并给它赋予我的变量上下文。它只是试图让它回来,我有困难,当我使用你的self.originalInterfaceController(self.InterfaceController对我来说)它给了我错误“属性'InterfaceController'找不到类型'SecondController'的对象。所以我想知道你是如何运作的 我为我的经验不足而道歉我感谢你的耐心等待 – Rohan