Use of Xcode slider output in calculation

Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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)sliderChanged:(id)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!!!
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Moved to a more appropriate forum.
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Threads merged - Do not cross post the same question to multiple forums.
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
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..
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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)sliderChanged:(UISlider*)sender{ double sliderValue = [sender value];}

Best regards,
Terry
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
The code you just posted is not correct, look at the example I showed.

Regards Mark
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
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..
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
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?
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
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:mad:"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
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Hi Terry

Sorry I did not get back to you sooner, I only look in on this forum a couple of times a week.

But there was some errors in the last code you posted, it needs to be more like this, although there are several ways to achieve the same thing.

Code:
//Interface.h file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    
}

@property (nonatomic, readwrite, assign) double sliderDoubleValue;

@property (nonatomic, readwrite, copy) NSString *angle1;
@property (nonatomic, readwrite, copy) NSString *angle2;

@property (nonatomic, readonly, strong) IBOutlet UITextField *textField1;
@property (nonatomic, readonly, strong) UITextField *textField2;
@property (nonatomic, readonly, strong) IBOutlet UISlider *mySlider;

- (IBAction)sliderChanged:(id)sender;

@end


//Implementation.m file

#import "ViewController.h"

@implementation ViewController

@synthesize textField1, textField2, mySlider, sliderDoubleValue, angle1, angle2;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [mySlider setMinimumValue:0.0];
    [mySlider setMaximumValue:100.0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sliderChanged:(id)sender
{
    if (sender == mySlider) {
        
        sliderDoubleValue = [mySlider value];
        
        NSNumber *sliderNumberValue = [NSNumber numberWithDouble:[mySlider value]];
        
        angle1 = [NSString stringWithFormat:@"%f", sliderDoubleValue];
        [self setAngle2:[NSString stringWithFormat:@"%@", sliderNumberValue]];
        
        [textField1 setText:[NSString stringWithFormat:@"angle1 = %@", angle1]];
        [textField2 setText:[NSString stringWithFormat:@"angle2 = %@", angle2]];
    }
}

@end

I have used properties in this example, but you could also use instance variables in your interface file, or local variables declared in a Method.

If you want to change the name of your properties as you did in your last posting, then change the name in the @synthesize area, like this.

Code:
// In interface file

@property (nonatomic, readonly, strong) IBOutlet UISlider *mySlider;

//In implementation file

@synthesize mySlider = _mySlider;

//Then you can access the slider like this

[_mySlider setValue: 50.00];

Hope this helps a bit more

Regards Mark
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
Mark,

Thanks much for working on this, again!

When I implement your property method code as follows:

-----------------------------------------------------------------------------------------
- (IBAction)sliderChanged:(id)sender
{
if (sender == mySlider) {

sliderDoubleValue = [mySlider value];

angle1 = [NSString stringWithFormat:mad:"%f", sliderDoubleValue];

[text1:[NSString stringWithFormat:mad:"angle1 = %@", angle1]];

}

}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{..................... etc, etc. .....


-------------------------------------------------------

I get the following error, when I try to use "angle1" in an equation further on in the "-(void)viewDidLoad" method: (No quotes in code.)

"Invalid operands to binary expression ('NSString*' and 'int')"


What's wrong?

(I would like to have the "- (IBAction)sliderChanged:(id)sender" method separate from the method containing the equation. Do I need to use an instance variable instead of using properties?)


Best regards,

Terry
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
You have to look at the code Terry, angle1 and angle2 in my example are NSString types, so the error is saying you cant do math with a string, if you want to do math then you dont need to turn them into strings, but leave then as either NSNumber, or primitive double or float types.

So in short you get the value of the slider into a number, then assign that number into a global variable, or property, then you can access it from any method in the same class, and do math with it.

In order for me to show you with code, you have to explain very clearly what your trying to achieve, and how.

Regards Mark
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
If you want to do math then do it something like this, please note Terry these examples I'm posting are in a very simplified form, just to show the concepts, and there are several other ways to do this sort of thing.

Code:
//interface.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    
}

@property (nonatomic, readwrite, assign) double sliderDoubleValue;

@property (nonatomic, readonly, strong) IBOutlet UITextField *myTextField;
@property (nonatomic, readonly, strong) IBOutlet UISlider *mySlider;

- (IBAction)sliderChanged:(id)sender;

- (double)multiply:(double)firstNumber:(double)secondNumber;

@end

//implementation.m

#import "ViewController.h"

@implementation ViewController

@synthesize myTextField, mySlider = _mySlider, sliderDoubleValue;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [_mySlider setMinimumValue:0.0];
    [_mySlider setMaximumValue:100.0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)sliderChanged:(id)sender
{
    if (sender == _mySlider) {
        
        sliderDoubleValue = [_mySlider value];
        
        double myFixedDoubleValue = 25.00;
        
        double multiplyedValue = [self multiply:sliderDoubleValue :myFixedDoubleValue];
                
        [myTextField setText:[NSString stringWithFormat:@"multiplyedValue = %f", multiplyedValue]];
    }
}

- (double)multiply:(double)firstNumber:(double)secondNumber;
{
    return firstNumber * secondNumber;
}

@end

And yes you can use Instance variables instead of proerties, but you would have to create setter and getter methods for the instance variables, thats why people tend to use properties instead, becase the synthesize declaration automatically creates the setter and getter methods for you.

Also I would use NSNumber types instead of primitive C types, that way you can coerce doubles to ints, and ints to floats ect.

Regards Mark
 
OP
T
Joined
Nov 29, 2012
Messages
9
Reaction score
0
Points
1
Mark,

You've made everything clear and been very thorough, and as a result I've got it all working.
(It seems obvious now, but somehow, I got wrapped around the axial.)

You are a gifted and giving teacher. If you ever need any help with vibrations, acoustics, or aerospace engineering, perhaps I could return the favor.

Thanks again,

Terry
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top