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

问题描述

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

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

这是我的代码:

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


$ b - (void)updateSensitivity {


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

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

$ b


解决方案

你的整个代码示例演示的形式,它的工作原理如下

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

  #importSliderDemoViewController.h
#importSettingsData.h

@implementation SliderDemoViewController

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


- (void)updateSensitivity {

settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:sensitivitySlider.value];
NSLog(@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 file

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

现在这里 SettingsData 是一个数据模型类as

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

  @interface SettingsData:NSObject {
NSNumber * sensitivitySettingValue;


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

现在updateSensitivity的输出将会是

  2011-11-22 09:59:57.259 SliderDemo [691:207] Slider Value是: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对象。



我发现 ($ settings)


$ b $ p $ $ $ $ settingsData = [[SettingsData alloc] init];





$ b

除非你分配这个对象,否则你总会得到0作为float值, NSLog为其属性sensitivitySettingValue的值。如果你已经分配了这个对象,确保在你尝试设置或获取它的任何成员变量值之前分配它。


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).

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...

Here's my code:

-(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 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 file

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

Now here SettingsData is a datamodel class declared as

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

@interface SettingsData : NSObject {
    NSNumber* sensitivitySettingValue;
}

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

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

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

The only point I find you might be doing something wrong is not creating this object as

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

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.

这篇关于没有按照我的预期行事的滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:25