New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
iOS Development Discussion on developing apps for the iOS platform.

Use of Xcode slider output in calculation


Post Reply New Thread Subscribe

 
Thread Tools
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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!!!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,991
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,563
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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?
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,563
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
The code you just posted is not correct, look at the example I showed.

Regards Mark
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,563
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,563
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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.
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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
QUOTE Thanks
Toddler

 
Member Since: Nov 29, 2012
Posts: 9
Toddler is on a distinguished road

Toddler is offline
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
QUOTE Thanks

Post Reply New Thread Subscribe


« Need Suggestions Naming ViewController | "Reading rights" in the native iOS email app »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
FLTK and Xcode 4 Bubbagump00 OS X - Development and Darwin 0 03-19-2012 01:07 PM
Weird file sizes in Finder slapple OS X - Operating System 23 01-26-2012 11:12 AM
Simultanious audio output from both mini Displayport and Audio Output port? leeon Music, Audio, and Podcasting 2 11-19-2010 04:34 AM
No audio output from external input jeremy1 OS X - Operating System 2 09-13-2010 12:59 AM
A couple xCode questions SoulRed12 OS X - Development and Darwin 10 04-16-2010 10:30 PM

All times are GMT -4. The time now is 07:25 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?