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
我不這麼認爲。 sumTotal是一個UILabel。 OP的評論欄self.sumTotal.text = ...看起來更正確。 – danh
鄧肯 - 謝謝你,解決了這個問題,並讓我做我需要的。謝謝。 – flooie
@flooie,如果您認爲正確,您可以將答案標記爲正確。 (我沒有看到它是怎麼回事,代碼分配和int作爲總和,然後放棄它在堆棧上,它也錯誤地聲明委託爲「保留」,從不設置或引用它)。答案中沒有解決這些問題。 – danh