How to use UIStepper

拥有回忆 提交于 2019-11-27 10:45:40

问题


I am trying to work with UIStepper to increment or decrement an integer, but both "-" and "+" increase the integer! How can I recognize the "+" and "-" button?

In the UIStepper header file there are two UIButtons:

UIButton *_plusButton;
UIButton *_minusButton;

for example :

- (IBAction)changeValue:(id)sender 
{        
    UIStepper *stepper = (UIStepper *) sender;

    stepper.maximumValue = 10;
    stepper.minimumValue = 0;      
    if (stepper)
    {
        integer++;
        [label setText:[NSString stringWithFormat:@"%d",integer]];
     }
     else
     { 
         integer--;
         [label setText:[NSString stringWithFormat:@"%d",integer]];
     }

}

回答1:


You should ignore the ivars. They will not help you.

The UIStepper has a value property that you can query to figure out what the current value is. So your method could simply be:

- (IBAction)valueChanged:(UIStepper *)sender {
  double value = [sender value];

  [label setText:[NSString stringWithFormat:@"%d", (int)value]];
}



回答2:


UIStepper returns Double value, for swift version do this:

@IBAction func stepperValue(sender: UIStepper) {
    print("the stepper value is :\(sender.value)")
}



回答3:


Take the outlet of UIStepper:

@property (strong, nonatomic) IBOutlet UIStepper *stepper;

In viewDidLoad Method:

self.stepper.wraps=YES;

if YES, value wraps from min <-> max. default = NO

 self.stepper.autorepeat=YES;

if YES, press & hold repeatedly alters value. default = YES

Set the initial value to 0.

NSUInteger value= self.stepper.value;

self.label.text= [NSString stringWithFormat:@"%02lu",(unsigned long)value];

Set the Maximum value

self.stepper.maximumValue=50;

Take the action of UIStepper:

- (IBAction)valueDidChanged:(UIStepper *)sender {
//Whenever the stepper value increase and decrease the sender.value fetch the curent value of stepper
        NSUInteger value= sender.value;
        self.label.text= [NSString stringWithFormat:@"%02lu",value];
}



回答4:


Try

    stepper.maximumValue = 10.0;
    stepper.minimumValue = 0.0;


来源:https://stackoverflow.com/questions/7779443/how-to-use-uistepper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!