在视图控制器之间传递多个标签时遇到问题

Posted

技术标签:

【中文标题】在视图控制器之间传递多个标签时遇到问题【英文标题】:Trouble passing multiple labels between view controllers 【发布时间】:2014-01-11 18:55:34 【问题描述】:

我被困在委托、协议问题上,试图在两个视图控制器之间传递数据。

我能够传递数据,但是当我尝试更改用户输入时,将两个用户输入相加,然后传递该数据,我就卡住了。有人可以解释我在哪里犯了错误吗?非常感谢。

如果这是一个新手问题,我很抱歉。但我已经为此苦苦挣扎了一段时间。再次感谢。

我的 View Controller.m 文件 #import "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

查看控制器.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】:

已编辑,基于 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 方法中设置它

【讨论】:

我不这么认为。 sumTotal 是一个 UILabel。 OP 的注释行 self.sumTotal.text = ... 看起来更正确。 邓肯 - 谢谢你,这解决了问题,让我可以做我需要做的事情。谢谢。 @flooie,如果您认为答案正确,可以将答案标记为正确。 (我不明白它是怎么回事,代码分配和 int 作为总和然后放弃它在堆栈上。它还错误地将委托声明为“保留”并且从不设置或引用它)。答案中没有解决这些问题。 @danh,你在说什么?在这个线程中的任何地方都没有提到委托。此外,OP 使用的是 ARC,因此也没有提及保留。此外,我编写的方法 calcTotal 将其结果作为方法结果返回,并且不会“将其丢弃在堆栈上”。 请在本页搜索“委托”。我不知道它是否是一个 ARC 项目,但请在此页面上搜索“保留”。您会在 OP 中找到两者。请查看您在名为 setTheValues 的方法中发布的代码。它计算一个 int 并将其丢弃在堆栈上。 (另外,请不要将其视为侮辱。我可以看到您的回答在这里很有帮助,只是想考虑下一位可能从中学到错误的读者)。

以上是关于在视图控制器之间传递多个标签时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

无法在表视图和另一个视图控制器之间传递数据

快速在标签查看的控制器之间传递数据?

在视图控制器之间的 NSString 中传递数据 - 目标 C [重复]

在视图控制器之间传递NSString中的数据 - 目标C [重复]

在 xamarin.ios 中的视图控制器之间传递数据

使用导航栏按钮项和核心数据在视图控制器之间传递日期