UISteppers - How to set a Stepper to restart from last value - ios

In an application with more than one view controller, how can you setup a UIStepper so that it restarts from the last selected value when the button is pressed.
Currently if I move back and forth to my view with the Steppers, if I press the stepper button it resets the value in my label to its start default value.
I'd like it to increase/decrease from the last value selected by the user.
Thanks!

try:
-(void)viewDidLoad {
int number = [[NSUserDefault standardUserDefault] integerForKey:#"value"];
if(!number) { number = 1; };
yourStep.value = number;
}
-(IBAction)changeValue:(UIStepper *)sender {
double value = [sender value];
int myValue = (int)value;
[[NSUserDefault standardUserDefault] setInteger:myValue forKey#"value"];
}

Related

iOS - How to Detect if Button is Pressed Twice

I have a button that you can press and the text of that button will change. I want to be able to detect when the user clicks the button again, so that the button text will change back to its original text. How would I be able to do that? Here is the code I have so far.
//Set Text and alignment for the buttons
[nocavities setTitle:#"No cavities\n5 points" forState:UIControlStateNormal];
nocavities.titleLabel.textAlignment = NSTextAlignmentCenter;
-(IBAction)nocavitiesaction:(id)sender {
[sender setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
}
I feel like in the IBAction I should change the state to
UIControlStateNormalPressed
but I'm not sure.
You can set a counter inside the IBAction
Something like this:
-(IBAction)nocavitiesaction:(id)sender {
static int count = 0;
count ++;
if(count % 2 != 0){
//Set title 1
}else{
//Set title 2
}
}
Open xib file and select the button.
Under the attributes inspector you will find "State Config", select "Default" from dropdown.
Change the title of button to "No cavities\n5 points"
Change State Config to "Selected"
Change title to "Whenever you visit our office cavity-free, you will receive 5 points!"
Now toggle these titles by.
- (IBAction)nocavitiesaction:(id)sender
{
UIButton * button = sender;
button.selected = !button.selected;
}
like this.
declare global int flag=1;
-(IBAction)methodName:(id)sender
{
if(flag==1)
{
//set title 1
flag=2;
}
else
{
//set title 2
flag=1
}
}
There's no UIControlStateNormalPressed state, but there is a UIControlStateSelected. You can set a title for both the selected and normal states, then manually set the UIButton to its selected state like so:
- (void)viewDidLoad {
[self.button setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
[self.button setTitle:#"No cavities\n5 points" forState:UIControlStateSelected];
}
- (IBAction)nocavitiesaction:(UIButton*)sender {
sender.selected = !sender.selected;
}
Note: I've changed the parameter from id to UIButton to avoid having to convert id to UIButton to check its selected state. And the UIButton should be of type "Custom" if you'd like to prevent the background tint while selected.

Changing the text of a label on button press with dynamic integer

I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!
You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.
-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
First You Can Set static int count = 0;

UIStepper iOS 7

I am new in programming, let's say I am still swimming in the ocean. I am trying to set up an UIStepper for change a value on a text field. This value has to change using the decimal number (step value = 0.1), I have read many documents but still not sure from where need to start.
The following is the code I am using:
- (IBAction)valueChanged:(UIStepper *)sender {
sender.stepValue = 0.1;
float value = [sender value];
self.txtRspeed.text = [NSString stringWithFormat:#"%.0f", value];
seems like I get the step Value that I want 0.1 but doesn't show up in the simulator. I also tried setting as initial value the one in the text field with some code but I still encounter errors.
What I am doing wrong?
- (IBAction)valueChanged:(UIStepper *)sender {
_value = txtRspeed.text.floatValue;
sender.value = self.stepper.stepValue + _value;
self.txtRspeed.text = [NSString stringWithFormat:#"%g", sender.value];
}
(void)viewDidLoad{
[super viewDidLoad];
self.stepper.value = 0.1f;
self.stepper.minimumValue = -100.0f;
self.stepper.maximumValue = 100.0f;
self.stepper.stepValue = 0.1f;
}
I am getting the increment but no the decrement.
If a set the stepper value in a viewDidLoad it start from 0
So create the UIStepper and set its stepValue property to 0.1. You can get the current value of the control using it's value property.
You'll probably want to create the stepper in your storyboard or .xib file and connect it to an IBOutlet in a view controller so that your view controller can access it easily.

iOS: Moving a uibutton by its tag outside of where it was generated

Is it possible? I have an array of buttons created in an void and I want to move one when a neighbouring one is called in the buttonTapped void. I have no trouble moving the button thats pressed because it is the sender, but I also need to move the one next to it and can't seem to get it to move. Each button in the array has a tag value so they're unique.
Thanks
Reasonable intuitive assumption: you generated the buttons so that they are ordered in the array...
- (void)moveButton:(UIButton *)sender // whatever
{
NSUInteger idx = [buttonArray indexOfObject:sender] + 1;
UIButton *nextButton = idx < buttonArray.count ? [buttonArray objectAtIndex:idx] : nil;
// do something with `nextButton`
}

UISlider value changed update result

I have three labels receiving output from three UISliders. A result is calculated and placed in a fourth label when a segmented control is touched.
How do I make the result update if one of the slider values is changed without touching the segmented control again?
First, add an event for your slider changing values:
[mySlider addTarget:self
action:#selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
You will want to do this for each slider.
In your sliderValueChanged: function:
- (void)sliderValueChanged:(id)sender {
[self calculateResult];
}
In calculateResult you can do your calculations and set your result box.
If you need to make sure your segmented control is in some state first, just add an if clause to sliderValueChanged:
- (void)sliderValueChanged:(id)sender {
if ([mySegmentedControl selectedSegmentIndex] == 1) {
[self calculateResult];
}
}
I came up with something that worked using mopsled's answer:
if (UIControlEventValueChanged && mySeg.selectedSegmentIndex == 0) {
float floatResult = [f3.text floatValue]-[f2.text floatValue];
float coldFactor = [fcold.text floatValue]*floatResult;
float result = [f1.text floatValue]+coldFactor;
NSString *tpResult = [[NSString alloc] initWithFormat:#"%.0f",result];
f4Result.text = tpResult;
}

Resources