2014-01-11 27 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

相关问题