0
我正在嘗試做我自己的間隔訓練計時器。現在,我在讀出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值。但是,當我嘗試直接讀取步進器值時沒有(發件人)發件人,我總是得到值0.
如
-(void)readStepperValue {
NSLog(@"Read UIStepper control value: %f", [intervalStepper value]);
}
我想這只是一個愚蠢的錯誤初學者...
好吧,我只是做了一個超級簡單的測試程序,代碼如下:
vierContro ller.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內,它無法讀取控制器的值。...
您是否在IB中連接了'intervalStepper'實例變量以及操作? –
我將步進控制器連接到IBAction。但是讀出控制者的價值與IB沒有任何關係,對吧? –
假設我正確地理解了代碼,在'-readStepperValue'中,它看起來像是在嘗試從名爲'intervalStepper'的UIStepper實例變量中讀取數據。如果這意味着指向您在IB中創建的UIStepper(而不是以編程方式),那麼它也需要與IB連接 - 將實例變量聲明爲「IBOutlet」並將其掛起。 –