UISlider to Set a Value

Discussion in 'iOS Programming' started by PanzyCat, Jul 31, 2010.

  1. macrumors newbie

    Joined:
    Jul 7, 2010
    #1
    Hey guys I'm having a little trouble making a UISlider set a value.

    here is my code:
    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.
     
  2. macrumors member

    Joined:
    Aug 10, 2007
    #2
    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?
     
  3. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #3
    Nothing happens when i run it and change the slider value. It stays the same.
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    #define does a substitution. So, what you end up with is:
    Code:
    slide.value = slide.value;
    That's not what you want.
     
  5. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #5
    So i should put a numerical value in the place of define?

    so #define kGravity 4
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     
  7. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     
  9. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #9
    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!
     
  10. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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?
     
  11. thread starter macrumors newbie

    Joined:
    Jul 7, 2010
    #11
    Yes, I want the user to be able to make the kGravity value whatever they wnat it to be.
     
  12. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    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?
     

Share This Page