2013-03-31 25 views
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內,它無法讀取控制器的值。...

+0

您是否在IB中連接了'intervalStepper'實例變量以及操作? –

+0

我將步進控制器連接到IBAction。但是讀出控制者的價值與IB沒有任何關係,對吧? –

+0

假設我正確地理解了代碼,在'-readStepperValue'中,它看起來像是在嘗試從名爲'intervalStepper'的UIStepper實例變量中讀取數據。如果這意味着指向您在IB中創建的UIStepper(而不是以編程方式),那麼它也需要與IB連接 - 將實例變量聲明爲「IBOutlet」並將其掛起。 –

回答

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 

擺脫實例變量的,只有依靠性能,不僅削減了你需要編寫的代碼量,但也有助於消除任何iVar /屬性綁定錯誤。

更新:self.stepper.value是一個雙不是int(但那不是你的問題)。