Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Question Use of Xcode slider output in calculation
    I'm an old dog trying to learn some new tricks (Objective C , Xcode, development of an app, etc.), and I'm having a lot of trouble getting a slider value into a calculation. (Most of the tutorials on the web illustrate how to put the slider value into a text box or label, but I want to use the slider value to scale accelerometer data (say, "rawaccelX"), which are float values.

    One problem that I have is that I don't really know how to specify the output of the method "- (IBAction)sliderChangedid)sender". Another problem is that when I try to multiply the slider value times "rawaccelX", I keep getting the message "Invalid operands to binary expression ('ULabel' and 'double')".

    Would someone please provide me a snippet of code that will do this? Thanks much!!!

  2. #2
    Use of Xcode slider output in calculation
    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,395
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    Moved to a more appropriate forum.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  3. #3
    Use of Xcode slider output in calculation
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,184
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    Threads merged - Do not cross post the same question to multiple forums.
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  4. #4
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    As you can see, I'm new to the forums, and I am sorry to have placed my thread inappropriately.

    But, I now believe that my thread belongs in the "IOS Development" forum. So, would you please consolidate/move my thread to there?

  5. #5
    Use of Xcode slider output in calculation
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,184
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    Done..I don't have access to my Mac right now or I would've whipped up a quick example for you..I'll take a look at it tonight..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  6. #6
    Use of Xcode slider output in calculation
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    The reason your getting that error is because your asking for the slider for it's value, which is
    actually the slider cells "Title", what you need to get is the slider's floatValue:, like this.

    Code:
    - (IBAction)sliderChanged:(id)sender
    {
        NSNumber *sliderValue = [NSNumber numberWithFloat:[sender floatValue]];
        [textField setStringValue:[NSString stringWithFormat:@"Slider floatValue = %@", sliderValue]];
    }
    in this example you can see that I'm asking for the sender's floatValue, not the value.
    Also the textField is an IBOutlet linked to a textField in the xib file, and the IBAction method
    is linked to the NSSlider control also in the xib file.

    You can get and set the floatValue: for the slider control lke this.

    Code:
    [mySlider setFloatValue: 75.00];
    Also you can set its min and max values with the setMinValue: & setMaxValue: methods.
    Like this

    Code:
    [mySlider setMinValue: 0.0];
    [mySlider setMaxValue:100.0];
    as for using the sliders floatValue to pass to other classes like the UIAccellerometer, you
    can also set a primitive float type to the slider's floatValue, like this.

    Code:
    float sliderFloat = [mySlider floatValue];
    Finally you can use floats and doubles in Objective-C Methods interchangably, so any
    class method wanting a double as an argument, will also accept a float, and vice versa.

    Hope this helps.

    Regards Mark

  7. #7
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Ashwin and Mark,

    Thanks much for your prompt replies and help. Mark, I'll try your code tonight or tomorrow, as I'm still having problems multiplying the acceleration data by the sliderValue that I get from: (IBAction)sliderChangedUISlider*)sender{ double sliderValue = [sender value];}

    Best regards,
    Terry

  8. #8
    Use of Xcode slider output in calculation
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    The code you just posted is not correct, look at the example I showed.

    Regards Mark

  9. #9
    Use of Xcode slider output in calculation
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,184
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    Also, you want to keep the sender as (id) type, do not set your function to accept the final object type. You then should do a isKindOfClass call to confirm that it's a UISlider object and then cast sender and then retrieve the value as Mark has suggested..
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  10. #10
    Use of Xcode slider output in calculation
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    I owe you big apologies Toddler, I have given you code for an OSX project, using a
    NSSlider, instead I was meant to be helping you with An iOS project with a UISlider, I'm
    working on too many cross platform projects, and contributing on to many developer forums.

    I Know that's no excuse, so I will start again, you are correct in getting a UISlider's value
    property, but in iOS projects you have to tell the method which value. like this.

    Code:
    - (IBAction)sliderChanged:(id)sender
    {
        if (sender == mySlider) {
            float sliderDoubleValue = [mySlider value];
            NSNumber *sliderNumberValue = [NSNumber numberWithFloat:[mySlider value]];
            [textField1 setText:[NSString stringWithFormat:@"sliderDoubleValue = %f", sliderDoubleValue]];
            [textField2 setText:[NSString stringWithFormat:@"sliderNumberValue = %@", sliderNumberValue]];
        }
    }
    As you can see from the example, I am checking if the value is from mySlider, which is a
    IBOutlet from my xib file, so are the textField1 and textField2 objects, also you can see that the value can be stored in a NSNumber class object, or primitive C type float.

    Also you can see that i'm using the setText: method, instead of the setStringValue: method
    used by NSTextField's, Doh!

    The reason you have to check which sender 's value you want, is that more than one UI
    control can be linked to the IBAction method, that's why your getting error warnings, it's
    saying the value from what!

    Also as in my last incorrect posting you can set the sliders min and max values with the
    setMinimumValue: and setMaximumValue: methods, like this.

    Code:
        [mySlider setMinimumValue:0.0];
        [mySlider setMaximumValue:100.0]
    You could see above that I can set the value returned from the UISlider to an NSNumber
    or a primitive float type, but if I had used a double type instead, I would not have got any
    complaints from Xcode, as previously stated, their interchangable with Objectie-C methods,
    but if you ever use ANSI C functions you have to send the correct type, I only point this out
    as some Cocoa Frameworks are based on the C language, and some on C++.

    I hope I did not confuse you with the OSX code.

    Regards Mark

  11. #11
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Mark,

    No problem on the miscue, as I haven't tried your code yet. Your precise and thorough explanations appeal to my nature, and I'm again optimistic that I'm going to master this -- eventually. But, as the wind is down here in NC this morning and perfect for flying model airplanes, I probably won't get to this until tonight or tomorrow.

    Best regards,

    Terry

  12. #12
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Mark,

    I tried to implement your code today, but I don't think that I got it right. (I assume that you're showing two different ways of getting the slider values?) When I look at sliderDoubleValue obtained with the following code, it is always zero. And, I get a warning that the local declaration of sliderDoubleValue hides the instance variable. (The slider values observed on the slider label range from 15 to 45.)

    In Interface file:
    Code:
    @property UISlider*mySlider;
    @property float sliderDoubleValue;
    
    -(IBAction)sliderChanged:(id)sender
    In Implementation file:
    Code:
    @synthesize sliderDoubleValue;
    
    -(IBAction)sliderChanged:(id)sender
    
     {
        if (sender == _mySlider) {
        float sliderDoubleValue = [_mySlider value];   
        [NSString stringWithFormat:@"sliderDoubleValue = %f",sliderDoubleValue];
        }
     }
    I will appreciate any further help you may provide.

    Best regards,

    Terry

  13. #13
    Use of Xcode slider output in calculation
    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,184
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3
    Terry, a couple of things. You should have a space between UISlider and *mySlider in your interface file. In your implementation file, your NSString is not being assigned to anything. You want to send that to the result to your text field or something?
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  14. #14
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Ashwin and Mark,

    Got a basic version working! On to some refinements and cleanup. (And obviously, I need to spend some time studying Objective-C.)

    Thanks much for your help and patience.

    Best regards,

    Terry

  15. #15
    Use of Xcode slider output in calculation

    Member Since
    Nov 29, 2012
    Posts
    9
    Mark and Ashwin,

    OK, with respect to assigning the NSString in my last post. Now I need to assign the string to an instance variable, say" Angle" that I can put into a equation in another method. However, when I write: "[Angle = [NSString stringWithFormat:@"sliderDoubleValue = %f",sliderDoubleValue]]", I get the error: "assigning to 'float' to incompatible type id."

    How do I assign the NSString to a float variable?

    Best regards,

    Terry

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Set variable to output of text field- xcode 4.5, applescript
    By dhmmjoph in forum OS X - Development and Darwin
    Replies: 15
    Last Post: 02-03-2013, 04:13 PM
  2. Use of Xcode slider output in calculation
    By Toddler in forum OS X - Development and Darwin
    Replies: 3
    Last Post: 11-30-2012, 09:37 AM
  3. Using Xcode slider output in a calculation.
    By Toddler in forum iOS and Apps
    Replies: 0
    Last Post: 11-30-2012, 09:10 AM
  4. Very slow 'info' calculation
    By zero2hero in forum OS X - Operating System
    Replies: 2
    Last Post: 11-03-2008, 05:09 AM
  5. CRC Values calculation
    By charu123 in forum OS X - Apps and Games
    Replies: 1
    Last Post: 05-26-2005, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •