How do I receive the current value of a slider in code?

18,061

Solution 1

For the first part of your question, it depends which OS you are using as to which process to use.

If you are using OS X, the best way to get a value for your slider is to create a binding in IB for your slider. To do this, click on the slider in IB and go to the bindings section. In the Value section, click on Value and select the class you wish to use the value in as your Bind To class (I'm going to use MyClass for this example). Then for the model key path, assign it to some value of your choice. For the purpose of this, I'll just call it sliderValue.

Then in MyClass.h, you must set up the following:

@interface MyClass : <your class type> {
    int sliderValue;
}
@property (readwrite, assign) int sliderValue;

In MyClass.m, you'll need to synthesize the value.

@synthesize sliderValue;

At this point, you should be able to get the value of your slider at any point in your code by calling [self sliderValue].

If you are, however, using iOS, then all you have to do is call the value property from your slider. So if you have the a UISlider *mySlider, all you have to do is call mySlider.value to get the current value of your slider.

For your second question, you can go about this two ways. If you want to append the strings, simply follow the format:

NSString *appendedString = @"";
appendedString = [appendedString stringByAppendingString:string1];

and so on until you have all your strings into your URL.

In your case, I would personally set up the entire URL string as a stringWithFormat:

NSString *urlString = [NSString stringWithFormat:@"http://www.starturl.com/%@%@%@", whatToSearch, middleOfURL, resultsStr];

Insert the values you want into the URL that way by setting up whatToSearch, etc., to the values you want. This way, you don't have to worry about appending everything together

Solution 2

    NSSlider * slider = [[NSSlider alloc] init];
    [slider setMinValue:50];
    [slider setMaxValue:150];
    int sliderValue = [slider intValue];

this doesn't put your slider on screen, but assume you made it in IB, ignore the first line, you can set your min max and get the value. you can make an action like

-(IBAction)sliderMoved:(id)sender

then bind that to the slider, if you set the slider to continuous you will get updates every time that it moves other wise just when you let go of the slider

-(IBAction)sliderMoved:(id)sender
{
    sliderValue = [slider intValue];
    [self doSomethingElseNow];
}

Solution 3

Your code should look like this:

NSString *urlString = [NSString stringWithFormat:@"http://starturl.com/?q=%@&max-results=%f", whatToSearch, [slider floatValue]];

EDIT: Corrected my answer. For UISlider it should be value property. For NSSlider you should use - floatValue method

Share:
18,061
Steve
Author by

Steve

Updated on June 05, 2022

Comments

  • Steve
    Steve almost 2 years

    I am (attempting) developing my first application in Xcode using cocoa framework.

    I have a slider which min value is 10 and max is 50. This is to select max search results.

    I have linked a label on my user interface to display the value of the slider and when it is moved, it updates the label on the user interface.
    However, I am trying to join around 4 strings to create my final URL one of them is the value of said label.

    I am trying to read the value of the label on the interface for use in creating the finished URL

    NSString *startofURL = @"http://starturl.com/?q=";  
    NSString *searchTerm = whatToSearch;  
    NSString *middleofURL = "&max-results=";  
    NSString *resultsStr = labelMaxResults.stringValue;    //Problem here ??  
    

    I have 2 questions; firstly, How do I go about retrieving the value of my slider via code instead of trying to get it from the linked label, as I think this is my problem.

    secondly, I have read up on joining and appending strings, however I am a little confused on which is the best method to use in order to join up the 4 strings into one long URL.

  • Nico
    Nico almost 13 years
    Only if slider is a UISlider, not an NSSlider.
  • Nico
    Nico almost 13 years
    Since middleOfURL is a constant string, you can integrate it into the format string, as Kashiv showed in his answer.
  • akashivskyy
    akashivskyy almost 13 years
    Ah, I thought that this is UI element... So this should be [slider floatValue] instead.
  • JeremyP
    JeremyP almost 13 years
    Actually for NSSlider -doubleValue is probably better.
  • bauerMusic
    bauerMusic over 8 years
    In short (for NSSlider) : [slider floatValue], [slider intValue] etc.