滑块的行为不像我预期的那样

Slider Not Behaving As I Would Expect(滑块的行为不像我预期的那样)

本文介绍了滑块的行为不像我预期的那样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滑块可以将浮点数从 1 更改为 10,但我想保存此值并在所有视图控制器中使用它,因此我将该浮点数保存为模型类中的 NSNumber (settingsData.sensitivitySliderSettingValue).

I have a slider that changes a float from 1 to 10 but I want to save this value and use it across all view controllers so I have saved that float as an NSNumber in a model class (settingsData.sensitivitySliderSettingValue).

我试图在每次更改时将更新后的滑块值输出到控制台,但是它只是设置为 0 而不是 0 到 10.我不明白为什么会这样......

I am trying to output the updated slider value to the console every time that it is changed however it just gets set to 0 rather than 0 to 10. I don't understand why this is...

这是我的代码:

-(IBAction)sensitivitySliderValueChanged:(id)sender
{
    [self updateSensitivity];    
}


-(void)updateSensitivity{


    settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:sensitivitySlider.value];

    NSLog(@"The Slider Value is: %1.1f", [settingsData.sensitivitySettingValue floatValue]);

}

推荐答案

我以示例演示的形式实现了你的整个代码,它的工作原理如下

I implemented your entire code in form of a sample demo and it works as follows

//-----------------.m 文件

//----------------- .m file

#import "SliderDemoViewController.h"
#import "SettingsData.h"

@implementation SliderDemoViewController


-(IBAction)sensitivitySliderValueChanged:(id)sender
{
    [self updateSensitivity];    
}

-(void)updateSensitivity{

    settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:sensitivitySlider.value];
    NSLog(@"The Slider Value is: %1.1f", [settingsData.sensitivitySettingValue floatValue]);
    NSLog(@"sensitivitySlider.value= %f",sensitivitySlider.value);
    NSLog(@"sensitivitySlider.minimumValue= %f",sensitivitySlider.minimumValue);
    NSLog(@"sensitivitySlider.maximumValue= %f",sensitivitySlider.maximumValue);
}
@end

//---------.h文件

//----------.h file

#import <UIKit/UIKit.h>
@class SettingsData;
@interface SliderDemoViewController : UIViewController {
    IBOutlet    UISlider*   sensitivitySlider;
    SettingsData* settingsData;
}
-(IBAction)sensitivitySliderValueChanged:(id)sender;
@end

现在这里 SettingsData 是一个声明为

Now here SettingsData is a datamodel class declared as

//-----------

//-----------

@interface SettingsData : NSObject {
    NSNumber* sensitivitySettingValue;
}

@property (nonatomic, retain) NSNumber *sensitivitySettingValue;
@end

现在 updateSensitivity 的输出将是

Now the output of updateSensitivity will be something as

2011-11-22 09:59:57.259 SliderDemo[691:207] The Slider Value is: 0.0
2011-11-22 09:59:57.260 SliderDemo[691:207] sensitivitySlider.value= 5.581448
2011-11-22 09:59:57.261 SliderDemo[691:207] sensitivitySlider.minimumValue= 1.000000
2011-11-22 09:59:57.261 SliderDemo[691:207] sensitivitySlider.maximumValue= 10.000000

如果您查看日志的第一行它将始终输出 0.00,因为我们没有在任何地方分配 settingsData 对象.

If you look at the first line of logs it will always output 0.00 because we have nowhere allocated settingsData object.

我发现你可能做错的唯一一点是没有创建这个对象

if(!settingsData)
{
   settingsData = [[SettingsData alloc]init];
}

除非您分配此对象,否则您将始终获得 0 作为浮点值,同时对其属性 SensitivitySettingValue 的值执行 NSLog.如果您在某处分配了该对象,请确保在尝试设置或检索其任何成员变量值之前分配它.

unless you allocate this object , you will always get 0 as float value while doing an NSLog for the value for its property sensitivitySettingValue . In case you have allocated this object somewhere make sure you allocate it much before you try to set or retrieve any of its member variable value.

这篇关于滑块的行为不像我预期的那样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:滑块的行为不像我预期的那样

基础教程推荐