无法读取UIStepper的值

时间:2013-03-31 10:43:56

标签: ios uistepper

我正在尝试编写我自己的间歇训练计时器。现在我在读出UIStepper控件的值时遇到了问题。

我在UIStepper控制器上设置了一个int,它到目前为止一直有效。

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

}
到目前为止一切顺利。问题是,我希望一旦达到0就将计时器重置为UISteppers值。但是当我尝试直接读取步进器值而没有(id)发送器时,我总是得到0的值。

e.g

-(void)readStepperValue {

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

}

我想这只是一个愚蠢的初学者错误...


好的,我刚制作了一个超级简单的测试应用程序,代码如下:

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

标签从IBAction方法中正确更新。但是当我调用一个单独的方法(showValue)时,它不在IBAction中,它无法读取控制器的值....

1 个答案:

答案 0 :(得分:0)

请尝试这种方式。

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

摆脱iVars并且只依赖于属性不仅减少了你需要编写的代码量,而且还有助于消除任何iVar /属性绑定错误。

更新:self.stepper.value是double而不是int(但这不是你的问题)。