【发布时间】: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
【问题讨论】:
标签: ios objective-c delegates protocols viewcontroller