2014-01-11 53 views
0

我被困在委託人,協議問題中,試圖在兩個視圖控制器之間傳遞數據。在視圖控制器之間傳遞多個標籤時遇到問題

我能夠傳遞數據,但是當我嘗試更改用戶輸入時,通過一起添加兩個用戶輸入,然後通過該數據傳遞我卡住了。有人可以解釋我犯了什麼錯誤嗎?非常感謝你。

對不起,如果這是一個新手問題。但我現在一直在努力。再次感謝你。

我查看Controller.m或者文件 #進口 「ViewController.h」

@interface ViewController() 

@end 

@implementation ViewController 

-(void)setTheValues 
{ 
int numberOne = [_numberOne.text intValue]; 
int numberTwo = [_numberTwo.text intValue]; 
int sumTotal = (numberOne+numberTwo); 
sumTotal = [_sumTotal.text intValue]; 
// self.sumTotal.text = [NSString stringWithFormat:@"%d", sumTotal]; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if([[segue identifier]isEqualToString:@"vc2"]) 
{ 
SecondViewController *vc2 = [segue destinationViewController]; 
NSString *passedMessage = _textField.text; 
vc2.passedMessage = passedMessage; 
    [self setTheValues]; 
    NSString *passedMessageTwo = _sumTotal.text; 
    vc2.passedMessageTwo = passedMessageTwo; 

} 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)sendOver:(UIButton *)sender { 
    [self setTheValues]; 
} 
@end 

查看或者Controller.h

#import <UIKit/UIKit.h> 
#import "SecondViewController.h" 

@interface ViewController : UIViewController <sendMessage> 

- (IBAction)sendOver:(UIButton *)sender; 

@property (weak, nonatomic) IBOutlet UITextField *textField; 
@property (weak, nonatomic) IBOutlet UITextField *numberOne; 
@property (weak, nonatomic) IBOutlet UITextField *numberTwo; 

// declaring sumTotal 
@property (weak, nonatomic) IBOutlet UILabel *sumTotal; 
@end 

我的第二個視圖或者Controller.h #進口

@protocol sendMessage <NSObject> 

@end 

@interface SecondViewController : UIViewController 

- (IBAction)goBack:(UIButton *)sender; 
@property (weak, nonatomic) IBOutlet UILabel *label; 

@property (weak, nonatomic) IBOutlet UILabel *labelTwo; 

//delegate property 

@property(retain)id <sendMessage> delegate; 
@property(nonatomic, strong)NSString *passedMessage; 
@property(nonatomic, strong)NSString *passedMessageTwo; 
@property(nonatomic, strong)NSString *passedMessageThree; 

@end 

回答

1

EDITED,基於danh發出的指出:

它在我看來像你的setTheValues方法是錯誤的。這是你的(不正確)代碼:

-(void)setTheValues 
{ 
    int numberOne = [_numberOne.text intValue]; 
    int numberTwo = [_numberTwo.text intValue]; 

    //This line adds the wales of the 2 strings. That's good. 
    int sumTotal = (numberOne+numberTwo); 

    //This line throws away the value from the last line 
    //and replaces it with the contents of _sumTotal. That's not right. 
    sumTotal = [_sumTotal.text intValue]; 
} 

更好地做到這樣的:

-(int) calcTotal; 
{ 
    int numberOne = [_numberOne.text intValue]; 
    int numberTwo = [_numberTwo.text intValue]; 

    //This line adds the wales of the 2 strings. That's good. 
    int sumTotal = (numberOne+numberTwo); 
    return sumTotal; 
} 

然後重寫你的prepareForSegue方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([[segue identifier]isEqualToString:@"vc2"]) 
    { 
    SecondViewController *vc2 = [segue destinationViewController]; 

    vc2.delegate = self; 

    NSString *passedMessage = _textField.text; 
    vc2.passedMessage = passedMessage; 

    //Call calcTotal to read the inputs and return their sum as an integer. 
    int total = [self calcTotal]; 

    //convert the sum to a string. 
    NSString *passedMessageTwo = [NSString stringWithFormat: "%d", total]; 
    vc2.passedMessageTwo = passedMessageTwo; 
    } 
} 

順便說一句,您的代碼屬性中包含「保留」和「強」/「弱」限定符的混合。你不能那樣做。 「保留」用於手動引用計數代碼。 ARC中使用「強」和「弱」。如果你不使用ARC,你應該。切換到ARC,然後將該「保留」切換爲非原子,強。

您還有一個「委託」屬性(這是聲明爲保留的屬性),您從來沒有設置或使用過。你應該在你的prepareForSegue方法中設置它

+0

我不這麼認爲。 sumTotal是一個UILabel。 OP的評論欄self.sumTotal.text = ...看起來更正確。 – danh

+0

鄧肯 - 謝謝你,解決了這個問題,並讓我做我需要的。謝謝。 – flooie

+0

@flooie,如果您認爲正確,您可以將答案標記爲正確。 (我沒有看到它是怎麼回事,代碼分配和int作爲總和,然後放棄它在堆棧上,它也錯誤地聲明委託爲「保留」,從不設置或引用它)。答案中沒有解決這些問題。 – danh

相關問題