Slider Not Behaving As I Would Expect

此生再无相见时 提交于 2020-01-07 08:11:31

问题


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]);

}

回答1:


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.




回答2:


Have you NSLogged the raw sensitivitySlider.value to make sure the slider itself is setup correctly?




回答3:


make sure the minimumValue and maximumValue of the slider are setup correctly

and check you NSLog %1.1f look weird to me. try %f

for more troubleshooting please post output of this:

NSLog(@"sensitivitySlider.value= %f",sensitivitySlider.value)
NSLog(@"sensitivitySlider.minimumValue= %f",sensitivitySlider.minimumValue)
NSLog(@"sensitivitySlider.maximumValue= %f",sensitivitySlider.maximumValue)


来源:https://stackoverflow.com/questions/8216139/slider-not-behaving-as-i-would-expect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!