我遇到代理,协议问题,试图在两个视图控制器之间传递数据。
我能够传递数据,但是当我尝试更改用户输入时,通过将两个用户输入一起添加,然后传递该数据,我就会卡住。有人可以解释我犯错的地方吗?谢谢你。
如果这是一个新手问题,我很抱歉。但是我现在已经在努力了一段时间了。再次感谢你。
我的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
我的第二个视图Controller.h #import
@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
答案 0 :(得分: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方法中设置它