I’m relatively new to Objective-C so this might be really simple to do: I have a text box in my application that displays an ammo count, so each time the user taps a fire button the number in the text box will go down by one (12 > 11 > 10, etc) to 0. I have tried using for and if statements, but they are not working (I may have used incorrect syntax). This is what I’m using right now, but obviously I need the
- (IBAction)fire {
[ammoField setText:@"11"];
}
- (IBAction)reload {
[ammoField setText: @"12"];
}
The simplest way would be to convert the text to a number, decrement that and the reset the text, i.e. replace the code in the fire method with:
But don’t do this, it will make baby Steve Jobs cry.
A better way would be to add a new variable to the class of type
UIIntegerthat tracks the the number of bullets, i.e.:Oh, and don’t forget to call
reloadat some point early on to ensure _ammoCount and ammoField get initialised.