Stepper value reset after loaded from coredate - iphone

I am building a UITabledetail view, which contains a stepper and a UILabel.
The uilabel will show the number of stepper pressed.
My problem comes when i used core data to save the value of the uilabel. e.g. the final value of the uilabel is 30.
When i load back the data, the uilabel showed 30 but, when i press the stepper again, the uilabel reset to 1 again.
Is there any way to make the stepper continue to count based on my saved value?
Below is my code.
- (IBAction)stepperValueChanged:(id)sender
{
double stepperValue = ourStepper.value;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}

- (IBAction)stepperValueChanged:(id)sender
{
NSString *tempString = [NSString stringWithFormat:#"30"];// you can pass here whatever data(stepper value) that you retrieve from core data...
double steppervalue = [tempString doubleValue];
double stepperValue = ourStepper.value+steppervalue;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}
Hope, this will help you..

Related

Delete Key in UILabel is not working when code is correct

I am creating a calculator app and the calculator screen is a UILabel. I am having trouble with a delete key. Here is my Code:
.h
IBOutlet UILabel *display;
.m
- (IBAction)Del
{
[display.text substringToIndex:display.text.length -1];
}
it has no errors and runs in the simulator perfectly but does not actually work. Can anyone help.
-substringToIndex: creates a copy of a part of the string to which it is sent.
What you have here creates such a string, and does nothing with it.
I suspect you want to assign that string to something, say, the display.text property:
- (IBAction)Del {
display.text = [display.text substringToIndex:display.text.length -1];
}
You are ignoring the return value of the substringToIndex: method and you do not update the label's text.
You want something like this:
- (IBAction)Del {
NSString *oldText = display.text;
if (oldText.length > 0) {
NSString *newText = [oldText substringToIndex:oldText - 1];
display.text = newText;
}
}
This also guards against trying to delete from an empty label.

Add numerical content of UITextFields

I have four separate UITextFields and I want to add the numerical value of them all and then display the content within a UILabel, below is current code:
- (void)updateString {
self.string1 = textField1.text;
self.string2 = textField2.text;
self.string3 = textField3.text;
self.string4 = textField4.text;
self.string5 = textField5.text;
label.text = self.total; // total is an NSString and label is a UILabel
}
I am unable to add together the numerical values within each textField1/2/3... and store the value within total and then update the label. Any suggestions?
NSString has a method on it -intValue. That is what you want to use.
Check the section "Getting Numeric Values" in the NSString documentation
int totalValue = [textField1.text intValue] + [textField2.text intValue]...;
label.text = [NSString stringWithFormat:#"The total value is %d", totalValue];

Slider / Text Box Exchange Data

I have a UISlider that has two text boxes attached to it. Think of it like a tip calculator. Think about a receipt: it has a spot for you to put the tip, and then the final value. Let's add a slider into the mix.
So now we have two text fields (tip percentage and tip amount) and the slider. When the slider moves it calls a method that updates the two text boxes according to the value that the person has selected with the slider.
[self.slider addTarget:self
action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
-(void)sliderValueChanged:(id)sender
{
tipPercent.text = [NSString stringWithFormat:#"%d", (int)slider.value];
tipPercentDbl = (int)slider.value;//tipPercent as Dbl
tipDbl = (totalDbl * (tipPercentDbl/100));//tip as Dbl
tip.text = [NSString stringWithFormat:#"%.2f", tipDbl];
tipPercent.text = [NSString stringWithFormat:#"%.0f", tipPercentDbl];
totalWithTipDbl = (totalDbl+tipDbl);
totalWithTip.text = [NSString stringWithFormat:#"%.2f", totalWithTipDbl];
}
This works perfectly. What I want to do now (and am having trouble figuring it out) is how to change the value when the text fields are changed. i.e. someone manually enters in their own tip, how to update the slider and the tip percentage; or if someone manually enters in a tip percentage, how to update the slider and the tip value.
What would be the best way to do this?
This is fairly easy. I don't know all the variables you used, so substitute your own when you try the code. When the keyboard resigns, call the method:
- (void)updateTheSliderAndTipPercentage; {
float percentage = tipTheyEntered / totalCost;
[slider setValue:percentage animated:YES];
percentage *= 100; // This is if you want the percentage in [0,100].
tipPercent.text = [NSString stringWithFormat:#"%f", percentage];
}
EDIT: In order to know when you call this method, simple check:
- (BOOL)textFieldShouldReturn:(UITextField *)textField; {
if(textField == tipTheyEnteredTextField){
[self updateTheSliderAndTipPercentage];
}
}
Hope that helps!

setting the objectForKey with a string

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];

Accessing a UITextField from an array in Objective-C

I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good. I want to reference those objects when the UISlider value changes. Right now I'm storing those objects in a NSMutableArray and accessing them like so from the sliderChanged method:
NSInteger labelIndex = [newText intValue];
labelIndex--;
NSUInteger firstValue = (int)0;
NSMutableArray *holeArray = [pointsArray objectAtIndex:labelIndex];
UITextField *textField = [textFieldArray objectAtIndex:firstValue];
NSString *newLabel1Text = [[NSString alloc] initWithString:[[holeArray objectAtIndex:firstValue] stringValue]];
[textField setText: newLabel1Text];
[newLabel1Text release];
Everything is working good, but the program crashes on the setText: method. The last message I get from the program is: [UILabel drawTextInRect:] and then I get a EXC_BAD_ACCESS failure.
I want to be able to acces that dynamically created UITextField, but I must be going about it the wrong way.
Thanks!
Uh, yea, you create a text field, but you aren't displaying the field itself, just creating it.
If you want to do what I think you want to do, I would just do if statements.
ex.
if (firstValue == 1)
{
fieldone.text = #"whatever";
}
else if (firstValue == 2)
{
fieldtwo.text = #"whatever";
}