无法将文本字段中的值从UIViewController设置为另一个类进行计算

时间:2012-07-18 11:51:14

标签: iphone

我无法将从UItextfields取得的值从Viewcontroller设置到另一个从这些值进行计算的类。

@interface InitialViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *phValue;
@property (weak, nonatomic) IBOutlet UITextField *pco2Value;
@property (weak, nonatomic) IBOutlet UITextField *hco3Value;
@property (weak, nonatomic) IBOutlet UITextField *sodiumValue;
@property (weak, nonatomic) IBOutlet UITextField *chlorideValue;
@property (nonatomic,retain)NSDecimalNumber* ph;
@property (nonatomic,retain) NSDecimalNumber* pco2;
@property (nonatomic,retain)NSDecimalNumber* hco3;
@property (nonatomic,retain)NSDecimalNumber* sodium; 
@property (nonatomic,retain)NSDecimalNumber* chloride;

- (IBAction)analyseValues:(UIButton *)sender;

@end

在实施中

-(void)values{
[self setPh:[[NSDecimalNumber alloc] initWithFloat:phValue.text.floatValue ]];

[self setPco2:[[NSDecimalNumber alloc] initWithFloat:pco2Value.text.floatValue]];
[self setHco3:[[NSDecimalNumber alloc ] initWithFloat:hco3Value.text.floatValue]];
[self setSodium:[[NSDecimalNumber alloc] initWithFloat:sodiumValue.text.floatValue] ];
[self setChloride:[[NSDecimalNumber alloc] initWithFloat:chlorideValue.text.floatValue] ];
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self values];}

然后我有另一个接受这些值的类,但在运行它运行的程序时却会分配从initialViewController获取的值。

#import <Foundation/Foundation.h>
#import "InitialViewController.h"

@interface AcidBaseCalculations : NSObject
@property (nonatomic) float ph;
@property (nonatomic) float pco2;
@property (nonatomic) float chloride;
@property (nonatomic) float sodium;
@property (nonatomic) float hco3;

@implementation AcidBaseCalculations

@synthesize ph,pco2,chloride,hco3,sodium;

-(void)calculations{
    InitialViewController *bvc = [[InitialViewController alloc] init]; 
ph = bvc.ph.floatValue ;

pco2 = bvc.pco2.floatValue;     
// these values are not being set although the program   runs successfully

hco3 = bvc.hco3.floatValue;
sodium = bvc.sodium.floatValue;
chloride = bvc.chloride.floatValue;

我想在这里使用这些新值,并在这个类中执行一些逻辑操作。

1 个答案:

答案 0 :(得分:1)

您正在使用以下行创建新的InitialViewController

 InitialViewController *bvc = [[InitialViewController alloc] init];

这是一个不同于其他/原始InitialViewController

的实例

你要么必须制作一个属性

 @property(nonatomic,strong)InitialViewController myInitialVC;

在分配/启动AcidBaseCalculations时设置此属性。 我们必须为你想要计算的所有变量建立属性,并在alloc / initing到

时设置它们

AcidBaseCalculations