UIStepper. Find out whether incremented or decreme

2019-04-08 10:52发布

Figuring out whether the plus or minus button was pressed in UIStepper I use this method:

- (void)stepperOneChanged:(UIStepper*)stepperOne

And I compare stepperOne.value with a global value saved in my TableView Class.
I dont think this is the right way.

So to clarify i will show the "bad" code i am using:

- (void)stepperOneChanged:(UIStepper*)stepperOne
{
      BOOL PlusButtonPressed=NO;  
      if(stepperOne.value>globalValue)  
      {   
          PlusButtonPressed =YES;  
      }  
      globalValue=stepperOne.value;

    ////do what you need to do with the PlusButtonPressed boolean
}

So what is the right way to do this? (without having to save global variables)

标签: ios uistepper
8条回答
混吃等死
2楼-- · 2019-04-08 11:23

First set the minimum and maximum values of the stepper to 0 and 1 and make sure Value Wraps is unchecked.

enter image description here

Then you can use the following code to check which arrow was clicked

-(IBAction)stepperAction:(NSStepper *)sender {
    if ([sender intValue] == 0) {
        NSLog(@"up");
    } else {
        sender.intValue = 0;
        NSLog(@"down");
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-08 11:28

Set the minimum to 0, maximum to 2, and the value to 1. Then:

- (IBAction)stepperAction:(UIStepper *)sender // value changed
{
    if ( sender.value == 0) {
        NSLog(@"down");
    } else { // else up
        NSLog(@"up");
    }
    sender.value = 1; // reset
}

If you have more than one stepper, set each tag differently, then still use this method and also test sender.tag.

查看更多
Summer. ? 凉城
4楼-- · 2019-04-08 11:28

Swift 2.0:

Declarations:

@IBOutlet weak var mapViewZoomStepper: UIStepper!
 var mapViewZoomStepperValue: Double = -1.0

When value changes :

    @IBAction func mapViewZoomStepperValueChanged(sender: AnyObject) {        

     if (mapViewZoomStepper.value  > mapViewZoomStepperValue)
      {
       print("increment")
       mapViewZoomStepperValue = mapViewZoomStepperValue + 1.0

      }
      else
      {
       print("decrement")
       mapViewZoomStepperValue = mapViewZoomStepper.value - 1.0 
      }

  print("compare //  stored Value :   \(mapViewZoomStepperValue)  && real time value : \(mapViewZoomStepper.value)")

     }

Another option is to limit maximum value to 1. So the uistepper will have two state - 0 and 1 .

Use a switch statement to differentiate:

switch (mapViewZoomStepper.value) {
  case 0:
    print("A")
    break;
case 1:
 print("B")
 break;

  default:
    break;
}
查看更多
冷血范
5楼-- · 2019-04-08 11:33

This is simple and shorter way to identify whether "+" clicked Or "-" clicked of UIStepper


//First You have to declare oldValue as an int (or long/float/NSInteger etc. etc.) in Header File 
//So that you can access globally to that particular implementation file

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

- (IBAction)stepperStep:(id)sender 
{
        if (stepperObj.value>oldValue) {
             oldValue=oldValue+1;
             //Your Code You Wanted To Perform On Increment
        }
       else {
             oldValue=oldValue-1;
             //Your Code You Wanted To Perform On Decrement
        }
}
查看更多
孤傲高冷的网名
6楼-- · 2019-04-08 11:36

Swift 3.0:

This worked for me:

In your stepper action do make sure you reset stepper.value to 0 after, this makes the stepper value -1 on negative press and 1 on positive press.

@IBAction func Stepper(_ sender: UIStepper) {
    if sender.value == 1.0{
        //positive side was pressed
    }else if sender.value == -1.0{
        //negative side was pressed
    }
    sender.value = 0  //resetting the stepper value so negative is -1 and positive is 1
}
查看更多
Bombasti
7楼-- · 2019-04-08 11:36
var sampleStepperValueForIncrement:Int = Int()


@IBAction func sampleStepperValueChanged(_ sender: UIStepper) {        

        if(Int(sender.value) > sampleStepperValueForIncrement){
            print("increasing")
            sampleStepperValueForIncrement += 1

        }
        else{
            print("decresing")
            sampleStepperValueForIncrement =  sampleStepperValueForIncrement - 1
        }
    }
查看更多
登录 后发表回答