How to use UIStepper

后端 未结 5 392
無奈伤痛
無奈伤痛 2020-12-13 09:06

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 \"-\

相关标签:
5条回答
  • 2020-12-13 09:36

    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]];
    }
    
    0 讨论(0)
  • 2020-12-13 09:41

    Try

        stepper.maximumValue = 10.0;
        stepper.minimumValue = 0.0;
    
    0 讨论(0)
  • 2020-12-13 09:48

    I think such an error is not possible in Xcode 11.2. But if you have found this topic in search "How to use stepper" here is an answer:

    1). Create a stepper on the storyboard, control drag it as always. It should create a referencing outlet.

    2). You need a function that is connected to this particular stepper, whose argument is sender: UIStepper. It should be something like

    @IBAction func stepperValueChange(_ sender: UIStepper) {}

    3). Now write what should happen when the function is executed (each time you press the stepper). For explanation lets use label. Control drag him as we always do. The below is in the function, this line is executed when the stepper is hit.

    valueLabel.text = Int(sender.value).description

    And that's it.

    P.S. Stepper has "special abilities", namely:

    -> Autorepeat means - holding stepper increases counter fast. True/false -> Wrap - values are in "circle". Dropping below smallest value will give the biggest. Exceeding the biggest - you drop to the lowest. The stepper is somehow looped. -> isContinuous. Better to make it always true.

    These properties i can change by choosing them in Attributes inspector.

    Also for the reference: https://www.ioscreator.com/tutorials/stepper-ios-tutorial

    0 讨论(0)
  • 2020-12-13 09:50

    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];
    }
    
    0 讨论(0)
  • 2020-12-13 09:53

    UIStepper returns Double value, for swift version do this:

    @IBAction func stepperValue(sender: UIStepper) {
        print("the stepper value is :\(sender.value)")
    }
    
    0 讨论(0)
提交回复
热议问题