开发者

Storing and opening data in a variable

I'm making an app tha开发者_如何转开发t calculates certain things.

I need it to be able to take the input from the first textfields, for example 4+4 and save the result in a variable.

In the second text fields there could be 8+8 for example, and the result of that will also be saved into a variable (possibly the same).

Third row of textfields could yield more numbers etc, etc..

In the end there will be a button "Calculate" for example. And that will take the results from first, second, third etc textfields and calculate all of those together and output the end result.

The calculations are of course more advanced than this, but I just need the basic/simple idea of how to do this.

There is no need for saving the data to a file just now, it should just be in the app while the other textfields are being filled.

For 0x8badf00d: Header.

@interface UnitConverterViewController : UIViewController {
NSMutableArray *calculationsArray;
UITextField         *m1Text;
UITextField         *m2Text;

}

@property (nonatomic, retain) IBOutlet UITextField *m1Text;
@property (nonatomic, retain) IBOutlet UITextField *m2Text;
@property (nonatomic, retain) IBOutlet NSMutableArray *calculationsArray;

@end

Implementation:

@implementation UnitConverterViewController
@synthesize m1Text, m2Text, calculationsArray;
@synthesize resultTotal = _resultTotal;

-(id)init {
if(self = [super init]){
    calculationsArray = [[NSMutableArray alloc] init];
}
}
- (void)compute{
NSString* sumString = [NSString stringWithFormat:@"%d",[m1Text.text intValue]+[m2Text.text intValue]];
[calculationsArray addObject:sumString];
}
-(IBAction)calculate{
int total=0;
for(NSString* sumStr in calculationsArray){
    total = total+[sumStr intValue];
}
NSLog(@"Total: %d", total);
[calculationsArray release], calculationsArray = nil;
}

I must be doing something wrong, and I know I need a way to output this, a label and such. But for now I need to know if what I've done so far is correct, and what the best way of outputting it would be.


You should declare the variables to store the results in your header file, these are than accessible from anywhere in your .m file, the same goes for your text fields.

For example:

Calculator.h

@interface Calculator: SuperclassName{
 UITextField *_fldOne;
 UITextField *_fldTwo;
 UITextField *_fldThree;
 UITextField *_fldFour;
 int resultOne;
 int resultTwo;
 int _resultTotal;
}

@property(nonatomic, readonly) int resultTotal;

- (void) calculate;
@end

Calculator.m

@implementation Calculator

@synthesize resultTotal = _resultTotal;

- (void) calculate{
    resultOne = [_fldOne.text intValue] * [_fldTwo.text intValue];
    resultTwo = [_fldThree.text intValue] / [_fldFour.text intValue];
    totalResult = resultOne + resultTwo;
 }




@end

In this example resultOne and Two, and all the textfields are available throughout your class to work with, the totalResult is set as a readonly property and synthesized to create a getter automaticaly (which returns the value stored in _totalResult because of synchronizing like totalResult = _totalResult) as so it is available to read from outside the class.

As long as it all happens on one screen it should be more than enough, but of course you could make an NSDictionary or NSArray but that seems unnecessary here.

Hope this helps


Save the result to array. Lets say you have NSMutableArray* calculationsArray;//iVar

//initialize calculationsArray in init method
-(id)init
{
   if(self = [super init])
   {
     calculationsArray = [[NSMutableArray alloc] init];
   }
}
- (void)compute
{
  NSString* sumString = [NSString stringWithFormat:@"%d",[textField1.text intValue]+[textField2.text intValue]);
  [calculationsArray addObject:sumString];
}
- (IBAction)calculate
{
  int total=0;
  for(NSString* sumStr in calculationsArray)
  {
    total = total+[sumStr intValue];
  }
  NSLog(@"Total: %d",total);
  [calculationsArray release],calculationsArray = nil;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜