0

I'm trying do program my own interval training timer. Now I have a problem with reading out the value of a UIStepper Control.

I set an int over the UIStepper controller, which works so far.

- (IBAction)stepperChange:(id)sender {

// converting double to int
int temp = [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue];

NSLog(@"stepper was pressed. Current values is %d", [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]);

self.intervalSeconds.text = [NSString stringWithFormat:@"%i", [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue]];

mainInt = [[NSNumber numberWithDouble:[(UIStepper *)sender value]] intValue];

}

so far so good. the problem is, that i want to reset the timer to the UISteppers value as soon as it reaches 0. But when I try to read the steppers Value directly without the (id)sender, I always get the value of 0.

e.g

-(void)readStepperValue {

NSLog(@"Read UIStepper control value: %f", [intervalStepper value]);

}

I guess it's just a stupid beginners mistake...


Ok, I just made a super simple test app, the code looks like this:

vierController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

IBOutlet UIStepper *stepper;
IBOutlet UILabel *stepperValue;
}

@property (nonatomic, strong) UIStepper *stepper;
@property (nonatomic, strong) UILabel *stepperValue;

- (IBAction)showStepperValue;

@end

viewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize stepper, stepperValue;

- (IBAction)showStepperValue {
int temp = stepper.value;
stepperValue.text = [NSString stringWithFormat:@"%i", temp];
[self showValue];
}


- (void)showValue {
int temp = stepper.value;
NSLog(@"stepper.value: %i", temp);
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

The label gets updated correctly from within the IBAction method. But when I call a seperate method (showValue), which is not within the IBAction it can't read the value of the controller....

meth
  • 1,887
  • 2
  • 18
  • 33
DaniS.
  • 181
  • 2
  • 10
  • Did you connect the `intervalStepper` instance variable in IB, as well as the action? – Rich Tolley Mar 31 '13 at 11:01
  • i connected the stepper controller to the IBAction. but reading out the value of the controller doesn't have anything to do with the IB, right? – DaniS. Mar 31 '13 at 11:08
  • Assuming I'm understanding the code correctly, in `-readStepperValue` it looks like you are trying to read from a UIStepper instance variable called `intervalStepper`. If this is meant to point to a UIStepper that you created in IB (rather than programmatically) it needs to be connected to IB as well-declare the instance variable as an `IBOutlet` and hook it up. – Rich Tolley Mar 31 '13 at 11:24
  • My guess is that "intervalStepper" is nil, because you never connected it. – Hot Licks Mar 31 '13 at 14:15

1 Answers1

0

Try it this way instead.

viewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIStepper *stepper;
@property (nonatomic, strong) IBOutlet UILabel *stepperValue;

- (IBAction)showStepperValue;

@end

viewController.m:

#import "ViewController.h"

@implementation ViewController

- (IBAction)showStepperValue {
    double temp = self.stepper.value;
    self.stepperValue.text = [NSString stringWithFormat:@"%f", temp];
    [self showValue];
}

- (void)showValue {
    double temp = self.stepper.value;
    NSLog(@"self.stepper.value: %f", temp);
}

@end

Getting rid of the iVars and only relying on properties not only cuts down on the amount of code you need to write, but also helps remove any iVar/property binding mistakes.

UPDATE: self.stepper.value is a double not an int (but that wasn't your problem).

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117