gpt4 book ai didi

ios - 在 View Controller 之间传递多个标签时遇到问题

转载 作者:行者123 更新时间:2023-11-29 03:21:13 25 4
gpt4 key购买 nike

我在尝试在两个 View Controller 之间传递数据时遇到委托(delegate)协议(protocol)问题。

我能够传递数据,但是当我尝试通过将两个用户输入加在一起然后传递该数据来更改用户输入时,我被卡住了。有人可以解释我在哪里犯了错误吗?谢谢你。

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

我的 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

查看 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

我的第二个 View 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

最佳答案

已编辑,基于 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,然后将“保留”切换到非原子、强。

您还有一个从未设置或使用过的“委托(delegate)”属性(即声明为保留的属性)。你应该在你的 prepareForSegue 方法中设置它

关于ios - 在 View Controller 之间传递多个标签时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com