PDA

View Full Version : UISlider to Set a Value




PanzyCat
Jul 31, 2010, 10:05 AM
Hey guys I'm having a little trouble making a UISlider set a value.

here is my code: #define kGravity slide.value




-(IBAction)changeGrav {

slide.value = kGravity;

}


Is there something that I'm doing wrong? I can't seem to get this to work.

Thanks and sorry for my elementary questions.



EnigMoiD
Jul 31, 2010, 01:37 PM
I'm not that familiar with #define, but it seems like you're using it wrong. You didn't give kGravity a data type, unless you defined it somewhere else. It should be a float between 0 and 1, I think. Just define it regularly.
Also, according to the Xcode documentation, you should use setValue:animated: to set the value, rather than changing the property manually.
What exactly did it do when you ran this code?

PanzyCat
Jul 31, 2010, 02:11 PM
I'm not that familiar with #define, but it seems like you're using it wrong. You didn't give kGravity a data type, unless you defined it somewhere else. It should be a float between 0 and 1, I think. Just define it regularly.
Also, according to the Xcode documentation, you should use setValue:animated: to set the value, rather than changing the property manually.
What exactly did it do when you ran this code?

Nothing happens when i run it and change the slider value. It stays the same.

dejo
Jul 31, 2010, 04:14 PM
#define does a substitution. So, what you end up with is:
slide.value = slide.value;
That's not what you want.

PanzyCat
Jul 31, 2010, 04:23 PM
#define does a substitution. So, what you end up with is:
slide.value = slide.value;
That's not what you want.

So i should put a numerical value in the place of define?

so #define kGravity 4

dejo
Jul 31, 2010, 04:34 PM
So i should put a numerical value in the place of define?

so #define kGravity 4
If you're trying to set your slider value to 4, yes, assuming, of course, that that value makes sense in terms of your slider's minimum and maximum values.

PanzyCat
Jul 31, 2010, 04:40 PM
When i do that, it doesn't work.

dejo
Aug 1, 2010, 01:06 AM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16)

Well, time to start debugging. What are the min and max values of your UISlider? Do you have slide properly connected? Any compile- or run-time errors? Etc.

PanzyCat
Aug 1, 2010, 08:58 AM
the min and max values of my slider are 0 and 1. But kGravity is .432.

Yes, I have it in the Interface Builder, connected to the slider.

The thing is, when i try to move the slider, it locks up in one certain position, and then it wont move.

And there are no errors that make the project fail. Even when im running it, no errors. The only thing is the error mentioned above.

Thanks for all of the help!

dejo
Aug 1, 2010, 11:02 AM
The thing is, when i try to move the slider, it locks up in one certain position, and then it wont move.
I would guess the certain position is the value .432. Why? Because, assuming changeGrav is connected to the valueChanged event of your slider, it's doing exactly what you're telling it to do: in response to a change in value, call changeGrav which then sets the slider's value to a constant, namely .432. Is there something else you want it to do?

PanzyCat
Aug 1, 2010, 02:01 PM
I would guess the certain position is the value .432. Why? Because, assuming changeGrav is connected to the valueChanged event of your slider, it's doing exactly what you're telling it to do: in response to a change in value, call changeGrav which then sets the slider's value to a constant, namely .432. Is there something else you want it to do?

Yes, I want the user to be able to make the kGravity value whatever they wnat it to be.

dejo
Aug 1, 2010, 04:49 PM
Yes, I want the user to be able to make the kGravity value whatever they wnat it to be.
Then you need kGravity to be a variable and not a define. And you need to rearrange the pieces in your assignment statement.

Let me ask: what is your experience level with programming in Objective-C or programming in general?