【问题标题】:can't read the value of a UIStepper无法读取 UIStepper 的值
【发布时间】:2013-03-31 10:43:56
【问题描述】:

我正在尝试编写自己的间歇训练计时器。现在我在读取 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)sender 的情况下直接读取步进器值时,我总是得到 0 的值。

例如

-(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 方法中正确更新。但是当我调用不在 IBAction 中的单独方法 (showValue) 时,它无法读取控制器的值....

【问题讨论】:

  • 你连接IB中的intervalStepper实例变量,以及动作了吗?
  • 我将步进控制器连接到 IBAction。但是读出controller的值跟IB没关系吧?
  • 假设我正确理解了代码,在 -readStepperValue 中,您似乎正试图从名为 intervalStepper 的 UIStepper 实例变量中读取数据。如果这意味着指向您在 IB 中创建的 UIStepper(而不是以编程方式),则它需要连接到 IB 以及将实例变量声明为 IBOutlet 并将其连接起来。
  • 我猜“intervalStepper”是零,因为你从来没有连接过它。

标签: ios uistepper


【解决方案1】:

试试这个方法。

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

摆脱 iVar 并仅依赖属性不仅可以减少您需要编写的代码量,还有助于消除任何 iVar/属性绑定错误。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多