Question

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....

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top