Help with UISlider - iphone

With my UISlider, if my value 'tipPercentage' gets to 10 or more, the label 'costWithTipLabel' gets set back to the textField's 'costWithoutTip' value starting with a 10% tip increase.
I would really appreciate it if you could take a look at my code and let me know of the problem causing this.
Thanks in advanced.
- (IBAction)aSliderChanged:(id)sender {
UISlider *slider = (UISlider *)sender;
if (slider == tipslide) {
NSString *tip = [NSString stringWithFormat:#"%.f", slider.value * 100];
float tipPercentage = [tip floatValue];
NSString *multiplier = [NSString stringWithFormat:#"1.%.f", tipPercentage];
[costWithTipLabel setText:[NSString stringWithFormat:#"%.2f", [[costWithoutTip text] floatValue] * [multiplier floatValue]]];
[tipTextLabel setText:[[NSString stringWithFormat:#"Tip (%.f", slider.value *100]stringByAppendingString:#"%):"]];
}
else if (slider == peopleslide) {
NSString *p = [NSString stringWithFormat:#"%.f", slider.value*10];
float numOfPeople = [p floatValue];
[numberOfPeopleTextLabel setText:[NSString stringWithFormat:#"Each (%.f):", numOfPeople]];
[numberOfPeopleLabel setText:[NSString stringWithFormat:#"%.2f", [[costWithTipLabel text] floatValue]/numOfPeople]];
}
[totalBillCost setText:[NSString stringWithFormat:#"%.2f", [[costWithTipLabel text] floatValue]]];
}

I expect it has to do with unexpected formatting of your strings. I suggest you break your stringWithFormat assignments into multiple simple (non-nested) statements, and then NSLog your values using as generic a format (%f) as possible. Then you should be able to track down where the problem is.

Related

how to convert value one format into the another format?

This is my Button method. When I press the button it's value is every time increment one & display into Label. Then it's reach 6 then convert like 1.0 , 7 = 1.1, 8 = 1.2 , 12 like 2.0 like cricket over format.
How can I do that?
-(void)OneNoBTNPressedMethod
{
// LBL it's my label & display the text
NSString * overStorage = LBL.text;
// perform the addition operation
CalcOperation operation;
operation = Plus;
//add one every time when we press the button
NSString * overOneBTNStr = [NSString stringWithFormat:#"1"];
NSString *overVal = overOneBTNStr;
LBL.text= [NSString stringWithFormat:#"%qi",[overVal longLongValue]+[overStorage longLongValue]];
}
Thanx in advance..
It worked for me. Hope it works for you as well. Assuming your LBL contains "1" as initial text. hope it helps
- OneNoBTNPressedMethod {
NSString *str = LBL.text;
NSArray *arr = [str componentsSeparatedByString:#"."];
if ([arr count] == 1) {
if ([LBL.text intValue] >= 5) {
LBL.text = [NSString stringWithFormat:#"%i.%i",0,0];
} else {
LBL.text = [NSString stringWithFormat:#"%i",[LBL.text intValue] + 1];
}
} else if ([arr count] == 2) {
if ([[arr objectAtIndex:1] intValue] >= 5) {
int left = [[arr objectAtIndex:0] intValue] + 1;
LBL.text = [NSString stringWithFormat:#"%i.%i",left,0];
} else {
LBL.text = [NSString stringWithFormat:#"%i.%i",[[arr objectAtIndex:0] intValue],[[arr objectAtIndex:1] intValue] + 1];
}
}
}
Try this logic
Let `int num` is count
Then
numBeforeDecimal = num/6;
numAfterDecimal = num%6;
Concatenate both number
[NSString stringWithFormat: #"%d.%d",numBeforeDecimal,numAfterDecimal];

Conversion Fahrenheit to celsius programmatically

In my project, want to show the weather in fahrenheit first, then if the user wants clickes on conversion, needs to show the weather in celsius. My code is
NSNumber *metric = [[NSUserDefaults standardUserDefaults] objectForKey:#"metric"];
NSLog(#"Metric is %#", metric);
CGFloat aFloat = [speed floatValue];
CGFloat tFloat = [temperature floatValue];
CGFloat tempFloat = (tFloat-30)/2;
NSNumber * p_Number = [NSNumber numberWithFloat:tempFloat];
//Convert mph to kmph
if ([metric boolValue]) {
[windValueLabel setText:[NSString stringWithFormat:#"%.2f kmph", aFloat * 1.6] ];
temperatureLabel.text = [NSString stringWithFormat:#"%#", p_Number];
}
else{
[windValueLabel setText:[NSString stringWithFormat:#"%.2f mph", aFloat / 1.6]];
temperatureLabel.text = [NSString stringWithFormat:#"%#", temperature];
}
When u start the app, its working and showing temperature in fahrenheit, but crashes at celsius man... is that the current conversion. help me out guys
Your formula is slightly off, you want:
CGFloat tempFloat = (tFloat-32.0) / 1.8;
But that's not what making it crash. In fact, it's not crashing for me. What message do you get when it crashes?

How to display x raise to y in UIlabel

how can I display 5 raise to 1/3 in iphone i.e I want 1/3 written above 5 can anyone help please
I Found this solution, hope so it would be helpful for you.
x to the power of y in a UILabel could be easy. Just replace your indices with unicode superscript characters... I use the following method to turn an integer into a string with superscript characters.
+(NSString *)convertIntToSuperscript:(int)i
{
NSArray *array = [[NSArray alloc] initWithObjects:#"⁰", #"¹", #"²", #"³", #"⁴", #"⁵", #"⁶", #"⁷", #"⁸", #"⁹", nil];
if (i >= 0 && i <= 9) {
NSString *myString = [NSString stringWithFormat:#"%#", [array objectAtIndex:i]];
[array release];
return myString;
}
else {
NSString *base = [NSString stringWithFormat:#"%i", i];
NSMutableString *newString = [[NSMutableString alloc] init];
for (int b = 0; b<[base length]; b++) {
int temp = [[base substringWithRange:NSMakeRange(b, 1)] intValue];
[newString appendString:[array objectAtIndex:temp]];
}
[array release];
NSString *returnString = [NSString stringWithString:newString];
[newString release];
return returnString;
}
}
Try this NSString *cmsquare=#"cm\u00B2";
It will display cm².
Yes you can do that but you need custom UILabel, either Make it by yourself or Get it Open Source..

Retrieve UITextfField values and convert to inches with decimal?

If I have formatting for a textfield like:
//Formats the textfield based on the pickers.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *result = [feetArray objectAtIndex:[feetPicker selectedRowInComponent:0]];
result = [result stringByAppendingFormat:#"%#ft", [feetArray objectAtIndex:[feetPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:#" %#", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:0]]];
result = [result stringByAppendingFormat:#"%#", [inchArray objectAtIndex:[inchesPicker selectedRowInComponent:1]]];
result = [result stringByAppendingFormat:#" %#in", [fractionArray objectAtIndex:[fractionPicker selectedRowInComponent:0]]];
myTextField.text = result;
}
Which display's in the textfield like 00ft 00 0/16in How can I change that all to inches with decimal? I'll need to take the ft, and multiply by 12 = variable.Then add that to inches, as well as take my fraction 1/16 and divide that by 16 to get my decimal value and then add that to the inches so it shows like 1234.0625 in order to make my calculation. Can someone help me accomplish this? Thank you in advance!
NSString * theString = RiseTextField.text;
NSString * feetString = [theString substringWithRange:NSMakeRange(0, 2)];
NSString * inchesString = [theString substringWithRange:NSMakeRange(5, 2)];
NSUInteger rangeLength = ([theString length] == 14) ? 1 : 2;
NSString * fractionString = [theString substringWithRange:NSMakeRange(8, rangeLength)];
double totalInInches = [feetString doubleValue] * 12 + [inchesString doubleValue] + [fractionString doubleValue] / 16;
You can easily get the number that you want by doing the calculations with the numbers you have there. Once you've got the actual number, you should use a NSNumberFormatter to present it with the desired amount of decimals and format.
This should solve your problem. Or did you need help converting the strings to numbers so that you can add them together?

iPhone refactoring with ivar names

Could anyone tell me how I can dynamically specify an ivar name within my method?
l2 is the ivar I'm trying to target.
//this works
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:#"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:#"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
}
//this doesn't
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:#"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:#"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
sndMenuItem = [NSString stringWithFormat:#"l%d", i];
sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
sndMenuItem.userData = (id)i;
}
}
Thanks,
Mark
If you have it declared as a property, you may be able to use KVC to get it.
float h1 = [object height];
float h2 = [[object valueForKey:#"height"] floatValue];
[EDIT]
I didn't understand what you're saying. The answer is no. You can't specify a variable name dynamically. What you can do is this:
// if `l2` is a member of self. (as in self.l2)
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:#"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:#"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
key = [NSString stringWithFormat:#"l%d", i];
tmp = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
tmp.userData = (id)i;
[self setValue:tmp forKey:key];
}
}
[EDIT]
You should probably re-structure your entire class.
#interface myViewController: NSViewController {
UIButton *sound1;
UIButton *sound2;
SoundMenuItem *l1;
SoundMenuItem *l2;
}
#property (assign) IBOutlet UIButton *sound1; // connect up in IB
#property (assign) IBOutlet UIButton *sound2;
- (IBAction) clickSoundButton: (id)sender; // connect up to sound1 and sound2 in IB
- (SoundMenuItem) getSoundMenuItem: (int) i;
#end
#implementation myViewController
- (IBAction) clickSoundButton: (id)sender
{
if (sender == (id)sound1) l1 = [self getSoundMenuItem: 1];
if (sender == (id)sound2) l2 = [self getSoundMenuItem: 2];
}
- (SoundMenuItem) getSoundMenuItem: (int) i
{
if (maxunlocked <= i) return
NSString *filename = [NSString stringWithFormat:#"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
NSString *filenameHi = [NSString stringWithFormat:#"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
SoundMenuItem *sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
sndMenuItem.userData = (id)i;
return sndMenuItem; //(assuming it is auto-released)
}
#end
Sorry about the lack of info and thanks for your patience. I was struggling to add it all into the comments area due to the character limit.
This is what I'm attempting...
In my header file:
//SoundMenuItem is a class
SoundMenuItem *l1;
SoundMenuItem *l2; (I actually have 20 buttons, one for each game level)
In my .m file
//here I set up the l1 button which is never locked
l1 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
//and here I set the l2 button to be locked by default
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:#"levellock.png" selectedSpriteFrameName:#"levellockHi.png" target:self selector:#selector(doNothing:)];
//now I check to see if level2 has been unlocked (maxunlocked > 1) and if so I reset the l2 instance to use different images.
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:#"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:#"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:#selector(level:)];
}
So rather than having 20 iterations of the above if statement, one for each button I'm wanting to refactor it into one.
I hope I've made myself clearer.
After the question was clarified my first answer was not valid. The answer by #stephen with the suggestion for using a dictionary to keeping all the SoundMenuItem is what I would have proposed as well.