2015-10-19 48 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