Quote:
Originally Posted by alex5776
Hey,
How can I get my app to add and subtract based on a button pressed.
I have a text field with two buttons next to it. When I tap the top one i want the text field to display 1, tap again 2, as high as the user wants it to be.
The second buttons does it the opposite, tab it will go down, but I don't want it to go into negatives.
I have done NO math type things in iPhone development so I don't even know where to start.
Any help?
|
I would suggest using an integer or floating point instance variable in your view controller to hold the current value you display.
Set that instance variable up as a property with a custom setter.
In the setter for your value, display the new value to your text field. Something like this:
The header for your view controller:
Code:
@interface myViewController: UIViewController
{
NSInteger intValue;
IBOutlet UILabel* valueDisplay;
}
@property (nonatomic, assign) NSInteger intValue;
@end
The .m file for your view controller:
Code:
@implementation myViewController
@synthesize intValue;
- (void) setIntValue: (NSInteger) newValue;
{
if (intValue == newValue || intValue < 0)
return;
intValue = newValue;
//The line below takes the new intValue, converts it to a string,
//and displays it in the valueDisplay UILabel.
valueDisplay.text = [NSString stringWithFormat: @"%d", intValue];
}
(IBAction) addToButton;
{
self.intValue =self.intValue + 1;
}
(IBAction) subTractFromButton;
{
self.intValue =self.intValue - 1;
}
@end