UIStepper iOS 7 - ios

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.

Related

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;

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

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

Data from DetailViewController not displaying

I'm making a chemistry calculator segment in an app I'm working on, and I cannot get the data and I cannot get the information to correctly populate the screen. There is a secondary issue, the alignment, and if someone can help me with that I'd greatly appreciate it, but it isn't the primary focus of this question - I'll make a dedicated topic for it.
So I'll go over what I want to do, and what I've tried. What I'm trying to do is make a chemistry calculator where depending on what equation is selected, a UIStepper max/min.Value is modified to include all possible derivations of that equation, as well as certain UILabels and UITextFields shown/hidden.
I have confirmed that I have data passed down from the MasterViewController as I've set the data to an NSString called _equation, and successfully used _equation to modify the title of the DetailViewController under self.title in viewDidLoad.
I have tried placing and initializing all UIStepper properties appropriately under a if/if else nest under viewDidLoad (which also quantizes the _equationName possible values to an integer (eqNum) so that it can be used in a switch statement). I have also tried placing the UITextField hidden properties under viewDidLoad, to no avail.
So without further ado, let's get to the code. I've truncated the code down to one equation so you can see what's going on here easier - note that this is nested under the IBAction for the Calculate button:
// Take _equationName quantization and use it in a switch case to determine the formula that IBAction will use:
if (dflt)
{
switch (eqNum)
{
case 1:
if ((stepper.value = 1))
{
// Change deriv_units appropriately:
deriv_units.text = #"Energy (Joules)";
// This is a Planck's constant calculation, we hide the second variable as the constant
// is stored:
value2.hidden = YES;
value2_type.hidden = YES;
// Now we set up the parameters of the first entry variable:
value1_type.text = #"Frequency (in Hz)";
double frequency = [value1.text doubleValue];
double Planck = 6.626069e-34;
double energy = Planck * frequency;
// Now we set up the return field to return results:
NSString* resultIntermediate = [NSString stringWithFormat:#"%f", energy];
result.text = resultIntermediate;
units.text = #"J";
}
and the subsequent code under viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
self.title = _equationName;
int eqNum;
if ((_equationName = #"Energy-Frequency Relation"))
{
eqNum = 1;
// Set stepper bounds for derivation:
[stepper setMinimumValue:1];
[stepper setMaximumValue:3];
self.stepper.stepValue = 1;
self.stepper.wraps = NO;
self.stepper.autorepeat = NO;
self.stepper.continuous = YES;
// This is primarily a two-variable equation, so hide UITextView and UILabel #3:
value3.hidden = YES;
value3_type.hidden = YES;
}
(Props to anyone who recognizes this - it's Planck's relation! :D)
Here is what the GUI is supposed to look like (as it appears in Storyboard):
Here is what it comes out looking like in the iOS Simulator:
Note the misalignment issue, which isn't the principle issue in play here.
Also note that right now, the switch statement for equation parameters is under an if tree that checks to see if dflt (a Boolean variable assigned to UISwitch) returns true for double-precision calculations. However, upon toggling the switch ON, the issue does not correct.
Here's an even more complete explanation:
value#.text is the number entered in one of the three UITextFields, from top to bottom.
value#_type is the text to be displayed in the corresponding UILabel.
deriv_units is the UILabel below the one marked "Derivation Units", and is used to display which derivation of the equation has been selected using the UIStepper.
At bottom: The rightmost UILabel is the result label, whereas the leftmost is the units label.
Many thanks to anyone who can help this beginning developer along the path of programming righteousness.
About your alignment issue: it looks as though you are creating the storyboard for 4" screen, while running it on a 3.5" screen. In the storyboard onnthe lower right there are some small buttons, one of thise allows you to change instantly between the display for either 4" or 3.5".
Have you made sure your controls are connected to your code?
- (void) viewDidAppear: (BOOL) animated{
[super viewDidAppear: animated];
// some rude NSAsserts, but let's be sure
NSAssert(value1_type != nil, #"Your control element is nil, it might not be hooked up");
// you should now see this text in the textfield
value1_type.text = #"Frequency (in Hz)";
NSAssert(result != nil, #"Your control element is nil, it might not be hooked up");
result.text = #"some test text";
NSAssert(units != nil, #"Your control element is nil, it might not be hooked up");
units.text = #"J";
}

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

No value displayed in the text field

when i scroll the UISlider, i want to display the current value in UITextField, however i see the following string nan instead of the numeric value of the slider, can you help me please :
- (IBAction)sliderValueChanged:(id)sender
{
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [sender value]];
}
the connections in IB are correct, also, the min value for the slider is 0.0 and the max one is 70, i need to display the current position (i.e : 20) in the UItextField
Cast the sender to UISlider:
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [(UISlider *)sender value]];

Resources