I have three UITextFields. Two of them represent a certain number value. The third represents the percentage of the two. How do I setup the 3rd UITextField to do this simple math?
You can simply get the intValue or the floatValue or the doubleValue of the text that you have received from the first two text fields. Eg:
float firstFloat = [self.firstTextField.text floatValue];
float secondFloat = [self.secondTextField.text floatValue];
float answer = firstFloat / secondFloat; //or whatever math you need to do
self.thirdTextField.text = [NSString stringWithFormat:#"%.2f",answer];
Hope this helps.
Check out "Getting Numeric Values" here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
So you can use that to convert from the text property of your two UITextField instances.
Then you can convert them back to an NSString and plug them into the text of the third UITextField by using initWithFormat (something like [initWithFormat:#"%d", theResult]).
You can also do it in the following way.
NSString *str1,*str2;
str1=text1.text;
str2=text2.text;
int num1=[str1 intValue];
int num2 =[str2 intValue];
int ans=num+num2;
text3.text=[NSString stringWithFormat:#"%d",ans];
Hope this helps.
[answer setStringValue:[NSString stringWithFormat:#"%2.02g", answerFloat]];
Related
I want to change a UILabel with strings based on an array. Here is what I have so far:
- (IBAction) sliderValueChanged:(UISlider *)sender {
scanLabel.text = [NSString stringWithFormat:#" %.f", [sender value]];
NSString *wholeText = #"Very Bad";
self.scanLabel.text = wholeText;
}
Instead of just "Very Bad," I want different values to show "very Bad, Bad, Okay, Good, Very Good."
Where do I declare the NSMutableArray, and how do I implement it with the slider?
Update: Thank you all for your help! (I love stackoverflow)
This is not compiler checked...but you may get some idea.
NSArray *texts=[NSArray arrayWithObjects:#"very Bad", #"Bad", #"Okay", #"Good", #"Very Good",nil];
NSInteger sliderValue=[sender value]; //make the slider value in given range integer one.
self.scanLabel.text=[texts objectAtIndex:sliderValue];
You declare it in the same class as the above method.
Let's say it's called wholeTexts.
The code will look like this:
- (IBAction) sliderValueChanged:(UISlider *)sender {
scanLabel.text = [NSString stringWithFormat:#" %.f", [sender value]];
NSString *wholeText = [wholeTexts objectAtIndex:(wholeTexts.count - 1) * (int)(sender.value - sender.minimumValue)/(sender.maximumValue - sender.minimumValue)];
self.scanLabel.text = wholeText;
}
Put your text values into an array
NSArray* array = #[#"very Bad", #"Bad", #"Okay", #"Good", #"Very Good"];
set your slider to have a min value of 0 and maximum value of 4
turn your [sender value] into an int, eg floor([sender value])
pick an item from the array using your int, eg
NSString* result = [array objectAtIndex:myInt];
You can implement the array anywhere you want, as long as it's created before you need to use the slider. Sliders can only have a value between 0 and 1 in iOS, so you'll need to multiply the value by something to get numbers as high as the number of items in your array (minus 1), and you'll need to convert them to an integer, so you can use that value as the index into the array. Something like this:
- (IBAction) sliderValueChanged:(UISlider *)sender {
scanLabel.text = [myArray objectAtIndex:(int)(sender.value * 10)];
}
I'm new to iPhone development, so my apologies if this is a ridiculous question.
I'm following a sample from a book where it creates a exchange rate UIPicker and when you select one of the entries, it displays the conversion of US dollars into whatever currency is picked.
I want the value to be updated in my label before I start changing the value from the picker.
The very first time i enter my US dollar value in the textbox and I click on the "Return" button, i want the label to update and displays its equivalent value in whatever currency is currently selected in the picker.
I have the correct code in the didSelectRow event, and that part works as long as i start spinning the picker's component wheels, so I thought I'd take that code and put it in a function which would have one parameter i.e. row and this function would then be called from didSelectRow where the row parameter would be passed and then I would:
This is the code from the didSelectRow:
float rate = [[exchangeRates objectAtIndex:row] floatValue];
float dollars = [dollarText.text floatValue];
float result = dollars * rate;
NSString *resultString = [[NSString alloc] initWithFormat:#"%.2f USD = %.2f %#", dollars, result, [countryNames objectAtIndex:row]];
resultLabel.text = resultString;
[resultString release];
Call this function when the "Did End on Exit" event for the textbox but my I'm having two problems:
What is the correct way to write the function. Whatever way I write the function, I get various errors/warnings
How do i get the currently select row of the picker, so that I can pass it to this function when the "Did End on Exit" event occurs when i click on the "Return" button.
Thanks
T.
I am not sure, I completely understood you what exactly you are trying to do here. but still I am going ahead what you can do here is:
first of all declare an class variable of type int which will store the row index whenever didSelectRow delegate method will be called, mean time create a method like:
-(void)updateLabel:(int) rowIndex
{
float rate = [[exchangeRates objectAtIndex:row] floatValue];
float dollars = [dollarText.text floatValue];
float result = dollars * rate;
NSString *resultString = [[NSString alloc] initWithFormat:#"%.2f USD = %.2f %#", dollars, result, [countryNames objectAtIndex:rowIndex]]; // here it will go rowIndex which is the row value you stored in your variable
resultLabel.text = resultString;
[resultString release];
}
and call this method when you hit return.
I hope this helps you.
~Manoj
HI -
I have a value in a UILabel and I want to pass the number to a value that is of float type.
rate = float
hourlyRate = label
Below is my code and im getting errors. I know its bad form to go from object to primitive values but there has to be a way.
Any help would be appreciated!
rate = NSNumber numberWithFloat:[hourlyRate.text rate];
NSString has a convenient floatValue method:
rate = [hourlyRate.text floatValue];
rate = NSNumber numberWithFloat:[hourlyRate.text rate];
That’s invalid because if you’re trying to send the message numberWithFloat: to NSNumber it has to be enclosed in brackets, like so:
rate = [NSNumber numberWithFloat:[hourlyRate.text rate]];
And what’s more hourlyRate.text returns an NSString. You can’t send an NSString the method rate unless you subclassed it and added that method.
This is the right way to get the float value of UILabel, try this:
rate = [hourlyRate.text floatValue];
And do you mean:
float rate;
I am building an app where I need to change some helpful information based on what stage the user is at:
I used strings labeled Stage1 ... Stage7 in the dictionary and I want to display the helpful info from each Stage wen the user moves the slider.
NSDictionary *foodInfo = [foodArray objectAtIndex:row];
NSInteger numberLookup = lroundf([stageSlider value]);
NSString* stageText = #"Stage";
stageText = [stageText stringByAppendingFormat:#"%i", numberLookup];
NSNumber *helps = [foodInfo objectForKey:#"Stage1"]; // Need stageText string instead "Stage1" shown?
notesLabel.text = helps;
Currently I display the "Stage1" text no matter where the slider is positioned and I have verified that "stageText" is incrementing/decrementing just fine.
How do I put the stageText string in instead of the text string shown?
Thanks for the help.
padapa
Like that ? NSNumber *helps = [foodInfo objectForKey:[NSString stringWithFormat:#"stage%d",numberLookup]];
NSNumber *helps = [foodInfo objectForKey:stageText];
I'm putting up a CalculatorApp for the iPhone and there is just one thing that i need to end, the floating-point!
As in normal calculators, i need to do something so that will only permit one "." .
Can you dudes help me?
you have a few ways to go, such as, NSString's rangeOfString method, e.g.
#define FLOATING_POINT_STRING #"."; // set this to #"." or #"," according to the floating point type you want to use
float calculatorText = 45.194; // set this to whatever the label says, or you can skip the float => string conversion as shown below
NSString *text = [NSString stringWithFormat:#"%f", calculatorText];
if ([text rangeOfString:FLOATING_POINT_STRING].location != NSNotFound)
{
// do nothing, there is a floating point
}
else
{
// append FLOATING_POINT_STRING to the label
}
Good luck ;)
Are you using a UIButton for the decimal point button? If so, you could simply disable it as soon as it is pressed. And then of course, re-enable it when "clear" or "equals" or whatever is pressed:
[buttonDecimalPoint setEnabled: NO];