Little explication: I have an app with 5 textfields, i already got the code to make the avaerage of them. Using floates and giving a variable to each textfield.so, the adding of the value of all the textfield /5 (divided per 5)
My problem is that when a textfield is leaved emptty, its not goona be /5, if user fill in only 4 cells it will be/4 and so on.
Question: how to divide the adding of cells depending in how many cells have content?
I was tryng with textfield.text.length > 0 in an if. But i dont get it
Thanks, hope i was clear.
You could check each textfield and see if the text is NULL or blank (#"")
Use an int to keep track of the number of textfields that have a value.
For example:
int counter = 0;
float total = 0.0;
if([textfield1.text length] > 0)
{
counter++;
float thisValue = [textfield1.text floatValue];
total = total + thisValue;
}
repeat this for all 5 textfields and then divide your total by the counter variable.
You could include the if statement in a loop if you are going to vary the number of text fields or to reduce the amount of coding you do.
EDIT I have changed my answer based on the good suggestions of other to use text.length
try to count the number of used textfields by increasing a number every time you read out a textfield that has valid text in it and divide the result by that number. (logically the "number" should be initialized as 0)
Related
so i have a few images from 0.png to 9.png they will be used for the score.
now i want the images to be displayed based on the current score. I could do something like this:
scoreImage = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", score]];
the above code means that i have to create an infinite number of images to cover the score.
is there a more efficient way to do this? using only 10 images for each digit e.g 0.png 1.png 2.png... 9.png
Thanks in advance
The MOST efficient way to do this is to use a view object that displays text directly, like a UILabel. Then you can simply set it's font, size, and style to the desired values and set its text to the value you want.
You could craft a custom font that contains just numeric digits, and use that. I've never created a custom font, but I know it's possible.
Failing that, you could create a custom ScoreView class that contains 4 UIImageView objects. It would have an integer property score, and when you set the score value, it would install the correct digit images into it's different component image views.
The code to calculate the digit values might look like this:
int score = 7596;
int remainder = score;
int thousandsValue = remainder / 1000;
remainder -= thousandsValue*1000;
int hundredsValue = remainder /100;
remainder -= hundredsValue*100;
int tensValue = remainder/10;
int onesValue = remainder % 10;
NSLog(
#"For %d, thousandsValue = %d, hundredsValue = %d, tensValue = %d, onesValue = %d",
score,
thousandsValue,
hundredsValue,
tensValue,
onesValue
);
Then use those values to build image filenames for your thousands digit, hundreds digit, tens digit, and ones digits image views.
Finally, load and install the images into each imageView.
I am a beginner learning iOS development.
I am trying to limit the contents of a label marked "Factor" to only allow 2 whole integers between 0 and 99. I also want to force the user to enter at least two digits for every Factor attempt (i.e. "7" would be "07").
The label is not directly edited by the user. There is a number pad that the user can use to enter the Factor directly (just as you would on a calculator or remote control). There is also an Increment and Decrement button that modifies the label text.
I have limited the label so that it will not go above 99 or below 0 when the Increment or decrement buttons are pushed. But I am stuck on how to limit the user from pressing 3 or more numbers and that getting populated in the label text.
Current behavior:
Currently, if I press '9','0','1', that will result in '901'.
My goal would be to press '9','0','1', and see '1'.
In other words, more than 2 characters would never be displayed and once a third number is pushed, it will overwrite the existing two.
Here is what I am doing for my number buttons:
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if (self.numberCheck)
{ self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];
}
else
{
self.fDisp.text = number;
self.numberCheck = YES;
}
}
I think is is easy to accomplish that. See my solution below, I think it should work in your case.
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text stringByAppendingString:number];
}
You could simply check the length of the label string.
if ([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];
I have searched through the internet but I haven't find any straightforward answer. What I am trying to develop is basically a three same-object matching game. I have 3 UIButtons in one row. (each of these 3 buttons have a black hat icon).There are gonna be 3 unique types of hats. There are 3 rows of three items each. I want to touch the first hat and reveal a number from 0 to 2 (Let's say 1). After choosing the first hat, I want the second hat to generate a number between the 2 remained numbers( the choices are 0 and 2, let's say 2). And finally when I touch the third hat, it will generate the last remainder (number 0 in this example).The main reason for picking the numbers is because I want a certain number to represent a unique "Hat", so when I pick a hat with the number 1, a blue hat will pop out, with the number 0 a red hat etc... I have implemented the whole animation and stuff. I am just struggling in the "Unique random number picking". I am sure arrays will be a part of the "Random logic" but I haven't managed to implement it correctly... Any help would be HIGHLY appreciated :) Thank you all!
You can write a method that does this really simply with arc4random and a mutable array property that stores the numbers that have been shown as NSNumber objects.
-(NSInteger) randomNumberZeroToTwo {
NSInteger randomNumber = (NSInteger) arc4random_uniform(3); // picks between 0 and n-1 where n is 3 in this case, so it will return a result between 0 and 2
if ([self.mutableArrayContainingNumbers containsObject: [NSNumber numberWithInteger:randomNumber]])
[self randomNumberZeroToTwo] // call the method again and get a new object
} else {
// end case, it doesn't contain it so you have a number you can use
[self.mutableArrayContainingNumbers addObject: [NSNumber numberWithInteger:randomNumber]];
return randomNumber;
}
}
arc4random returns an NSUInteger so you have to cast it to avoid warnings with NSNumber.
Also make sure to instantiate your mutable array by adding this code so it does so automatically when self.mutableArrayContainingNumbers is called (i.e. lazy instantiation).
-(NSMutableArray *) mutableArrayContainingNumbers
{
if (!_mutableArrayContainingNumbers)
_mutableArrayContainingNumbers = [[NSMutableArray alloc] init];
return _mutableArrayContainingNumbers;
}
Have an array of pickable numbers.
pickable = #[0,1,2]; //Use NSNumbers but you get the idea.
/*Code to generate random number (rNum) with the range of 0-([pickable count]-1)*/
/*
Assign the number to your hat
and then remove that object from pickable
*/
[pickable removeObjectAtIndex:rNum]
and loop over that until [pickable count] == 0;
I hope that gives you a start good luck.
I'm building an IOS app. I want the user to input two options from a text field. One of the options will later be randomly displayed in a label. I'm thinking the text field should log the information in an NSMutableArray that I can later call on. The information only needs to be temporarily stored because it will go away after it is randomly displayed in the label.
I'm not sure how to do create this array from a text field. Any help is appreciated.
Is this what your looking for?
int choice = rand() % 2;
NSString* result;
if (choice == 0)
result = textField1.text;
else
result = textField2.text;
....
label.text = result;
So I have a TextField integer that needs to be added to a label.
For example when I type 20, it appears in the label and when I save it and come back it's still there, so far so good right, i got this part down.
Now lets say I come back and want to add another amount to whats already there, to the 20. how exactly would I go about doing it?
I used the NSUserDefaults to load/save the data but when I go to update the label I get stuck when trying to use my variables, say: currentValue, valueToAdd and valueTotalAfterAddition.
You have to get the intValue of the text, and then add whatever number. Example
int currentValue = textField.text.intValue;
int valueToAdd = 10; //change this to whatever you need
int valueTotalAfterAddition = currentValue + valueToAdd;
label.text = [NSString stringWithFormat:#"%d", valueTotalAfterAddition];