在ViewControllers之间减去Float值

时间:2013-02-20 04:25:58

标签: objective-c calculator subtraction

这是ViewControllerOne:

- (IBAction)total {

float a = ([textfield1.text floatValue]);
float b = ([textfield2.text floatValue]);
float c = ([textfield3.text floatValue]);
float d = ([textfield4.text floatValue]);
float e = ([textfield5.text floatValue]);
float f = a+b+c+d+e+([textfield6.text floatValue]);

total.text = [[NSString alloc] initWithFormat:@"$%.2f", f];

这是View ControllerTwo:

float g = ([textfield1.text floatValue]);
float h = ([textfield2.text floatValue]);
float i = ([textfield3.text floatValue]);
float j = ([textfield4.text floatValue]);
float k = ([textfield5.text floatValue]);
float l = a+b+c+d+e+([textfield6.text floatValue]);
totalTwo.text = [[NSString alloc] initWithFormat:@"$%.2f", l];

在ViewControllerThree中,我想用ViewControllerTwo的总和从ViewControllerOne中减去总和,并在标签中显示答案。

1 个答案:

答案 0 :(得分:0)

存储和total.text&的值totalTwo.text中的appDelegate喜欢

在AppDelegate.h文件中

float totalOne, totalTwo;
@propery(nonatomic)float totalOne, totalTwo;

在AppDelgate.m文件中,

@synthesize totalOne, totalTwo;

- (IBAction)total 
{
   //Create the instance of appDelegate as
   AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   float a = ([textfield1.text floatValue]);
   float b = ([textfield2.text floatValue]);
   float c = ([textfield3.text floatValue]);
   float d = ([textfield4.text floatValue]);
   float e = ([textfield5.text floatValue]);
   float f = a+b+c+d+e+([textfield6.text floatValue]);
   app.totalOne = f;
   total.text = [[NSString alloc] initWithFormat:@"$%.2f", f];
}

//Create the instance of appDelegate as
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
float g = ([textfield1.text floatValue]);
float h = ([textfield2.text floatValue]);
float i = ([textfield3.text floatValue]);
float j = ([textfield4.text floatValue]);
float k = ([textfield5.text floatValue]);
float l = a+b+c+d+e+([textfield6.text floatValue]);
app.totalTwo = l;
totalTwo.text = [[NSString alloc] initWithFormat:@"$%.2f", l];

-(IBAction)btnCalculate
{
    //Create the instance of appDelegate as
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    totalthree = app.totalOne - app.totalTwo;
    lbl.text = [NSString StringWithFormat:@"$%.2f", totalThree];
}