ios 如何使用 UIStepper
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7779443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use UIStepper
提问by Mc.Lover
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?
我正在尝试UIStepper
增加或减少整数,但“-”和“+”都会增加整数!如何识别“+”和“-”按钮?
In the UIStepper
header file there are two UIButton
s:
在UIStepper
头文件中有两个UIButton
:
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]];
}
}
回答by Dave DeLong
You should ignore the ivars. They will not help you.
你应该忽略 ivars。他们不会帮助你。
The UIStepper
has a value
property that you can query to figure out what the current value is. So your method could simply be:
该UIStepper
有一个value
属性,您可以查询找出当前值是什么。所以你的方法可以简单地是:
- (IBAction)valueChanged:(UIStepper *)sender {
double value = [sender value];
[label setText:[NSString stringWithFormat:@"%d", (int)value]];
}
回答by bikram sapkota
UIStepper returns Double value, for swift version do this:
UIStepper 返回 Double 值,对于 swift 版本,请执行以下操作:
@IBAction func stepperValue(sender: UIStepper) {
print("the stepper value is :\(sender.value)")
}
回答by Saurabh Jain
Take the outlet of UIStepper:
以UIStepper的outlet为例:
@property (strong, nonatomic) IBOutlet UIStepper *stepper;
In viewDidLoad Method:
在 viewDidLoad 方法中:
self.stepper.wraps=YES;
if YES, value wraps from min <-> max. default = NO
如果是,则值从 min <-> max 开始。默认 = 否
self.stepper.autorepeat=YES;
if YES, press & hold repeatedly alters value. default = YES
如果是,请反复按住更改值。默认 = 是
Set the initial value to 0.
将初始值设置为 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:
采取 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];
}
回答by Georgii Ivannikov
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:
我认为在 Xcode 11.2 中不可能出现这样的错误。但是,如果您在搜索“如何使用步进器”中找到了这个主题,这里有一个答案:
1). Create a stepper on the storyboard, control drag it as always. It should create a referencing outlet.
1)。在情节提要上创建一个步进器,像往常一样控制拖动它。它应该创建一个引用出口。
2). You need a function that is connected to this particular stepper, whose argument is sender: UIStepper. It should be something like
2)。您需要一个连接到这个特定步进器的函数,它的参数是发送者:UIStepper。它应该是这样的
@IBAction func stepperValueChange(_ sender: UIStepper) {}
@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.
3)。现在写下执行函数时应该发生什么(每次按下步进器)。为了说明,让我们使用标签。像往常一样控制拖着他。下面是在函数中,当步进器被击中时执行这一行。
valueLabel.text = Int(sender.value).description
valueLabel.text = Int(sender.value).description
And that's it.
就是这样。
P.S. Stepper has "special abilities", namely:
PS Stepper具有“特殊能力”,即:
-> 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.
-> Autorepeat 意味着 - 保持步进器会快速增加计数器。True/false -> Wrap - 值在“圆圈”中。低于最小值将给出最大的值。超过最大 - 你下降到最低。步进器以某种方式循环。-> 是连续的。最好让它永远真实。
These properties i can change by choosing them in Attributes inspector.
我可以通过在属性检查器中选择它们来更改这些属性。
Also for the reference: https://www.ioscreator.com/tutorials/stepper-ios-tutorial
也供参考:https: //www.ioscreator.com/tutorials/stepper-ios-tutorial
回答by James Thiele
Try
尝试
stepper.maximumValue = 10.0;
stepper.minimumValue = 0.0;