xcode 如何在标签中放置滑块值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19456012/
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 do I place a slider value in label?
提问by Peter Stuart
I am quite new to Xcode and I am having trouble placing the value of a slider in a label when the slider is value is changed.
我对 Xcode 很陌生,当滑块的值发生变化时,我无法将滑块的值放置在标签中。
Here is my code: Header file:
这是我的代码: 头文件:
@interface ViewTwoViewController : UIViewController
{
IBOutlet UISlider *slider;
IBOutlet UILabel *sliderLabel;
}
-(IBAction)sliderValue:(id)sender;
and here is my m file:
这是我的 m 文件:
-(IBAction)sliderValue:(UISlider *)sender {
sliderLabel.text = [NSString stringWithFormat:@"%g", slider.value];
}
I am not 100% sure why the value isn't updating?
我不是 100% 确定为什么值没有更新?
I am looking forward to hearing from you all!
我期待着收到大家的来信!
回答by bryanjclark
Here's an XCode project that does what you're looking for: http://clrk.it/013r203C1y1F
这是一个 XCode 项目,可以满足您的需求:http: //clrk.it/013r203C1y1F
Note that:
注意:
- The label and slider are hooked up to the
UIViewController
as IBOutlets in the storyboard. - The slider's
valueChanged
: method is linked to theUIViewController
in the storyboard. - The slider's value is displayed using
%f
.
- 标签和滑块连接到
UIViewController
情节提要中的 IBOutlets。 - 滑块的
valueChanged
: 方法链接到UIViewController
情节提要中的 。 - 滑块的值使用 显示
%f
。
Your method would look something like this:
你的方法看起来像这样:
-(IBAction)sliderValueChanged:(id)sender
{
if (sender == _slider) {
_label.text = [NSString stringWithFormat:@"%0.3f", _slider.value];
}
}
回答by Jonathan Arbogast
Here would be my process to figure out the root cause of this problem.
这是我找出此问题根本原因的过程。
- Did you remember to connect the
slider
andsliderLabel
outlets? - Did you remember to connect the IBAction to the slider? Set a breakpoint. When you drag the slider, does
sliderValue:
get called? What are the values ofslider
andsliderLabel
? - Its unusual that your
IBOutlet
s are not properties. I would expect to see:
- 你记得连接
slider
和sliderLabel
插座吗? - 您是否记得将 IBAction 连接到滑块?设置断点。当您拖动滑块时,是否
sliderValue:
会被调用?slider
和的值是sliderLabel
什么? - 您的
IBOutlet
s 不是属性,这很不寻常。我希望看到:
@property (weak, nonatomic) IBOutlet UISlider *slider;
and
@property (weak, nonatomic) IBOutlet UISlider *slider;
和
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
回答by Shivaay
I tried your code and seems to be correct..Please check these things:-
我试过你的代码,似乎是正确的..请检查这些东西:-
1) Have you connected Slider action Value Changed to IBAction method for proper functioning.
1) 您是否已将 Slider action 值更改为 IBAction 方法以正常运行。
2) Check our connections with Outlets.
2) 检查我们与奥特莱斯的联系。
Hope helps you out..
希望能帮到你。。