2013-07-02 55 views
3

如何才能知道用戶點擊了步進器的哪個按鈕(減號或加號按鈕)?UIStepper:如何知道用戶點擊了步進器的哪個按鈕(減號或加號按鈕)

- (IBAction)buttonStepper:(id)sender { 
    int stepperValue = self.outletStepper.value; 
    self.label.text = [NSString stringWithFormat:@"%d", stepperValue]; 
} 

感謝:3

+1

你可以保持由步進表示的值的二級存儲,然後看它是否上升或下降 –

+0

感謝您的回覆。我想知道是否有任何「本土」的方式來解決這個問題。否則該程序看起來並不那麼幹淨:P – vegligi

+0

爲什麼你需要知道? – Wain

回答

4

可以,而不是addTarget:動作,觀察步進值屬性,並要求在改變字典

{ 
    UIStepper *stepper = ...; 
    [stepper addObserver:self forKeyPath:@"value" 
       options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew 
       context:0]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if (object == stepper) { 
     double oldValue = change[NSKeyValueChangeOldKey]; 
     double newValue = change[NSKeyValueChangeNewKey]; 
     double change = newValue - oldValue; 
    } 
} 

或子類來接收新老值UIStepper並在重寫值集合中執行計算:

0
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    oldValue=stepperObj.value; 
} 

- (IBAction)stepperStep:(id)sender { 

if (stepperObj.value>oldValue) { 

    oldValue=oldValue+1; 
    NSLog(@"%d",oldValue); 
    //your code do you want to perform on increment 
} 
else 
{ 
    oldValue=oldValue-1; 
    NSLog(@"%d",oldValue); 
    //your code do you want to perform on decrement 
} 

}

你要申報的屬性oldValue在頭文件中的一個整數...

相關問題