Adding and subtracting numbers from UILabels - Xcode - iphone

I am trying to make an analysis app which calculates percentages of winners etc. Please see the attached picture:
So when there is a smash winner, the user clicks the UIStepper and it should 'add 1' onto the smash winners total and number of rallies, and also update the percentage which is the winner divided by the number of rallies. If there was a drop winner after, then drop winner total gets updated along with the number of rallies and the smash and drop winner percentages.
Hopefully I have explained it well enough for you guys to understand :/
I am using this bit of code to update a generic winner:
- (IBAction)netChanged:(id)sender {
self.netLabel.text = [NSString stringWithFormat:#"%d",
[[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]];
float net = [self.netLabel.text floatValue];
float rally = [self.rallyLabel.text floatValue];
float netPercentage = (rally == 0.0) ? 0 : net / rally * 100;
self.netPercentageLabel.text = [NSString stringWithFormat:#"%.2f%%", netPercentage];
}
My question is, what do I need to do in terms of code to update other percentages and rallies played when there is a winner?
Thanks.

You should be saving that data somewhere else, and not in the text property of a UILabel. Update the values, and then update the labels to reflect the updated values.
I highly recommend re-reading the documentation on Model-View-Controller.

Related

Weird behavior of for loop with multiple sprites in Sprite Kit

I am spawning 12 sprites to place in a game. Each sprite needs to have an original name. I am doing this by using a for loop. I have split the loop up to show you what is happening. So for the first couple of charges:
for (int i = -4; i <= 0; i++)
{
NSLog(#"i = %i", i);
[self spawnChargesWithNumber:i];
}
The NSLog tells me that i is -4, -3, -2, -1, 0. As it should. This calls the method spawnChargesWithNumber:i which looks like this:
- (void)spawnChargesWithNumber:(int)number
{
NSLog(#"number is %i", number);
_chargedBall = [[ChargedBall alloc] initWithImageNamed:[NSString stringWithFormat:#"ChargedBall%i", number]];
//code to make a random position
_chargedBall.position = CGPointMake(actualX, random);
_chargedBall.name = [NSString stringWithFormat:#"chargeNumber%i", number];
_chargedBall.zPosition = 15;
_chargedBall.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:13.0f];
_chargedBall.physicsBody.dynamic = YES;
_chargedBall.physicsBody.affectedByGravity = NO;
_chargedBall.physicsBody.mass = 0.1;
_chargedBall.velocity = CGPointMake(actualVelocityX, 0.0);
_chargedBall.physicsBody.categoryBitMask = chargeCategory;
_chargedBall.physicsBody.contactTestBitMask = playerCategory;
_chargedBall.physicsBody.collisionBitMask = playerCategory | selfCategory;
[self.map addChild:_chargedBall];
NSLog(#"You made charge with name %#", _chargedBall.name);
if (!_enemies) {
_enemies = [[NSMutableArray alloc] init];
}
[_enemies addObject:_chargedBall];
NSLog(#"you added %# to the array", _chargedBall.name);
}
I have an object called ChargedBall. The NSLog first tells me the number, which corresponds to i from above. Then it tells me I made charge (chargeNumberi) where it follows the same pattern. Then I add that chargedBall to an array. During the game if I collide with any of these, my NSLog from didBeginContact method tells me that I have contacted the proper charge. All is working as planned.
Since I actually have 12 charges and not 5 I have another for loop to show you. It is basically the same taking off where the other ended:
for (int i = 1; i <= 7; i++)
{
NSLog(#"i = %i", i);
[self spawnChargesWithNumber:i];
}
I did originally have these together, but I wanted to show you what was happening. After the call to spawnChargesWithNumber:i I get the rest of the charges. However, they are reversed! I don't get it. The NSLog still tells me the i, number, you created chargeNumberi, and added it to the array. I even NSLog the name of the image and it still corresponds. But chargeNumber7 shows picture 1, chargeNumber6shows picture 2, chargeNumber 5 shows picture 3, four is fine because there is an odd number of sprites to make, chargeNumber3 show picture 5, chargeNumber2 shows picture6 and chargeNumber 7 shows picture 1. So the entire thing is flipped. I have checked the pictures and they are all correct. This even happened when I had the for loop as just one loop, the positive charges were switched, but not zero to negative 4. Does anyone know why this would happen? I NSLog everything I could think of, I even changed the picture names, but nothing worked. If you need some more code from other places I can post, I am just so confused right now!
The problem has been solved. I really thought that I had programmed my for loop incorrectly. However, with the help of fuzzygoat and prototypical, I hardcoded everything, and used the debugger to find that all was as I intended. Therefore, prototypical suggested cleaning the derived data, as I had changed the image names after they had been imported to Xcode. I thought that cleaning the product did the same as deleting the derived data, but perhaps not. So thanks to everyone that looked and thanks to all that helped. You filled my brain with knowledge once again!

How to arrange decimal values one by one from right side to left side

I design setting for amount section so my requirements are: 1. the default Amount shall be $0.00. and 2. the max amount shall be $99999.99 and 3. when an amount is entered using the numerical keyboard,the amount shall get populated from right side of the decimal point (cents) to the left side of decimal point(Dollar). For e.g default value is $0.00 and when i put 1 then it should show $0.01 And when i put 2 it should show $0.12 and after that For 3 it should show $1.23 and so on to $99999.99.
I am not sure but i think this method should used
-(void)textFieldDidEndEditing:(UITextField *)textField
OR
- (BOOL)textFieldShouldReturn:(UITextField *)textField.
I am little bit weak in coding if possible please give me answer in code that would be better for me.
How can i get this? Need help.
Thanks
-(IBAction)btnPointPress:(id)sender
{
if([stringMain length] < 10)
{
stringMain = [[NSString stringWithFormat:stringMain]stringByAppendingFormat:#"."];
pointCheck = YES;
pointOnce++;
}
lastString = stringMain;
number = [stringMain doubleValue];
}
This is for adding a decimal point. Now tell me after this what is your requirement ?

How to convert from text to number integer?

i have input only number from text field and i want to change it to integer. what code should i write in?
just say text field say for 10 and i want to take that 10 = x, x = integer. so if i use it for mathematic process a = x + 1 and display say 11. is there any chance? i hope you understand what i mean. thank you. i think both of you will answer this simple question but i not because im beginner. im a 3d design graphic switch 180 degree become xcode programming. BTW ive tried this before but it cant.
NSInteger number = [[txtBtn text] integerValue];
Edited :
i've got this error, local declaration of 'number' hides instance variable, pointed to number at result = (number * 2) - 1; and i put the code like this.
NSInteger number = [[txtBtn text] intValue];
result = (number * 2) - 1;
i dont know whats wrong.
Are you sure it's a TextField and not a UIButton you are trying to get the integerValue from?
[txtBtn text]
I created a TextField and a UIButton, and implemented your code:
- (IBAction)btnClicked:(id)sender {
NSInteger i = [[inputField text] intValue];
int result = (i*2)-1;
NSLog(#"%i", result);
}
the output results is correct.
ex: type 5, output in console is 9.

Store user input, calculate and show in the end

I'm writing a program that calculates input a user gives, simple numbers. 2, 4, 7, 10, 12 and so on.
I need the program to store these inputs (that come in from different textfields) somehow. Use them in an internal equation, and then show them to the user in the end.
How do I do this? Please take note that I'm rather new at programming.
I know how to define textFields, do a calculate IBAction , and I've tried creating a -(void)calculate, which works, but only on 1 input. When I execute the following code, with input from 6 textfields instead of 3 (which are used in the first line (resultOne)) it gives me the wrong numbers. But I still need a better way of doing this.
-(void) calculate{
weightOne = [v1Text.text floatValue]/2;
weightTwo = [v2Text.text floatValue]/2;
resultOne = [m1Text.text floatValue] * weightOne + [s1Text.text floatValue] * weightOne;
resultTwo = [m2Text.text floatValue] * weightTwo + [s2Text.text floatValue] * weightTwo;
_resultTotal = (resultOne + resultTwo) / (weightOne + weightTwo);
NSString *resultString = [[NSString alloc]
initWithFormat: #"%.2f", resultOne];
resultLabel.text = resultString;
[resultString release];
}
I know that the above code outputs resultOne, but this is just for my own testing, to know that the equation works. But I would really like to start all fresh, since I don't believe that the above code is suitable for what I need it to do.
Please bare in mind that I am a beginner, and explain a little more, than just showing me some code.
I look forward to any help you can give me.
EDIT! - BELOW IS NEW CODE
-(IBAction) calculate:(id)sender {
weightOne = 1;
weightTwo = 0.75;
weightThree = 0.50;
mOne = [m1Text.text floatValue];
mTwo = [m2Text.text floatValue];
sOne = [s1Text.text floatValue];
sTwo = [s2Text.text floatValue];
resultOne = (mOne*weightOne) + (sOne*weightOne);
resultTwo = (mTwo*weightTwo) + (sTwo*weightTwo);
_resultTotal = (resultOne + resultTwo) / (weightOne*2 + weightTwo*2);
NSString *resultString = [[NSString alloc]
initWithFormat: #"Gennemsnit %.2f", _resultTotal];
resultLabel.text = resultString;
}
I decided to change my code, in order to easily calculate the input. Now I have another question.
Is it possible to save this equation in some way, so I can reuse it as many times as I want, without having to type the code again and again and again.
First of all calculate can be your IBAction method which will be called when tapped on the button. So change the prototype to -(IBAction)calculate:(id)sender.
Next if you are able to have outlets for the text fields, then there is no harm and absoulutely no difficulty in getting the text entered in them.
Few things you can do are to validate for correct input. [v1Text.text floatValue] is right expression for getting the numbers from text. You can also have a look at intvalue.
Now once you have done your computation, you can use the reference to the label/textfield where you want to display the result.
You can change that label exactly the way you did in your code.

How to automatically insert a decimal place on the iphone?

I know this has been asked a few times in the past but everything I try is failing. I have a custom numeric keypad with a UILabel. When I hit the '1' the UILabel displays a one. Now here's what I'm trying to do:
When I hit the '1' button I want: 0.01 in the UILabel
Followed by a '4' button I want: 0.14 in the UILabel
Then a '6': 1.46
etc, etc.
Now the keyboard and UILabel are working great. I just need to get the decimal's working now. Any sample code would be great, (and where I should place it in my application). Let me know if you want to see my code thus far. Pretty straightforward. Thanks everyone!
Here's about how I would do it:
#define NUMBER_OF_DECIMAL_PLACES 2
NSMutableArray *typedNumbers = ...; //this should be an ivar
double displayedNumber = 0.0f; //this should also be an ivar
//when the user types a number...
int typedNumber = ...; //the number typed by the user
[typedNumbers addObject:[NSNumber numberWithInt:typedNumber]];
displayedNumber *= 10.0f;
displayedNumber += (typedNumber * 1e-NUMBER_OF_DECIMAL_PLACES);
//show "displayedNumber" in your label with an NSNumberFormatter
//when the user hits the delete button:
int numberToDelete = [[typedNumbers lastObject] intValue];
[typedNumbers removeLastObject];
displayedNumber -= (numberToDelete * 1e-NUMBER_OF_DECIMAL_PLACES);
displayedNumber /= 10.0f;
//show "displayedNumber" in your label with an NSNumberFormatter
Typed in a browser. Caveat Implementor. Bonus points for using NSDecimal instead of a double.
Explanation of what's going on:
We're essentially doing bit shifting, but in base 10 instead of base 2. When the user types a number (ex: 6), we "shift" the existing number left one decimal place (ex: 0.000 => 00.00), multiply the typed number by 0.01 (6 => 0.06), and add that to our existing number (00.00 => 00.06). When the user types in another number (ex: 1), we do the same thing. Shift left (00.06 => 00.60), multiply the typed number by 0.01 (1 => 0.01), and add (00.60 => 00.61). The purpose of storing the number is an NSMutableArray is simply a convenience to make deletion easier. It's not necessary, however. Just a convenience.
When the user hits the delete button (if there is one), we take the last number entered (ex: 1), multiply it be 0.01 (1 => 0.01), subtract it from our number (0.61 => 0.6), and then shift our number right one decimal place (0.6 => 0.06). Repeat this as long as you've got numbers in your array of entered numbers. Disable the delete button when that array is empty.
The suggestion to use NSDecimal is simply to allow for very high precision numbers.