how to get the value of two sliders during sliding? - iphone

I have two sliders in my view and I want to get both value when I slide each one .
but whenever I try to do this the value of the other slider which is not sliding becomes zero and literally I get only one value.
this is what I have done
create two IBAction for each slider
create two property for each slider
#property (retain , nonatomic ) IBOutlet UISlider * slider2;
-(IBAction) slidingN : (id) sender
{
UISlider * si = (UISlider *) sender;
int value = (int) si.value;
NSString * newText = [[NSString alloc] initWithFormat:#"%i" , value];
myLable.text = newText;
int valueD = (int)slider2.value;
[self callSubView:value :valueD];
}
but the value is 0 how can I have both values?

Problem Solved (surpassingly) after restarting Xcode

Related

Assign string to UIButton's tag value

I wanted to give a string value to UIButton tag for unique identities.
I tried this code.But it doesnt work.
NSString *A;
NSString *B;
NSString *C;
NSString *D;
firstOptionABtn.tag=[A integerValue];
secondOptonBbtn.tag=[B integerValue];
thirdOptionCbtn.tag=[C integerValue];
fourthOptionDbtn.tag=[D integerValue];
- (IBAction)BtnClicked:(UIButton *)sender {
NSLog(#"%ld",(long)sender.tag);
}
I don't like this way but it will print 0 every time. Where am I going wrong?
Please help. Thanks
It returns zero as you did not initialize string, and it has null value when you convert it into integer it returns you zero. And it seems that you create your button from NIB so you can set their tag value from there and then get there value by implementing.
-( IBAction )buttonClicked:(id)sender
{
UIButton *btn= (UIButton *)sender;
NSLog(#"%d",btn.tag);
}
but if u want the output A,B,C, D then there is also a way
-(void)viewDidLoad{
btn1.accessibilityLabel=#"A";
btn2.accessibilityLabel=#"B";
btn3.accessibilityLabel=#"C";
btn4.accessibilityLabel=#"D";
}
-(IBAction)buttonClicked:(id)sender {
UIButton *btn= (UIButton *)sender;
NSLog(#"%#",btn.accessibilityLabel);
}
output is A, B, C, D
What need to use string for set tag value of button... why not set tag of Button statically simply set like :-
firstOptionABtn.tag=1;
First time seen this setting tag with string to int convert
In your queston First is you are not seting the value of you string
NSString *A;
NSString * B;
NSString * C;
NSString * D;
if you set value of string like
A=#"2"
then your code working and you got the value at the button click event
firstOptionABtn.tag=[A integerValue];
its output is
sender tag 2
Into you String value consider any Number value then the covert string to Intiger cosider only Number not alphabet character like
for example
If you set value of string like A=#"2aaabbb" then the output of button tag is 2 But if you set string value like A=#"bb2aaabbb" then output of button tag is Always 0 consider.
UPDATE
if you wish to set character to int
you can convert NSString to ASCII Like Bellow
A=#"A"
int ASCIINumber = [A characterAtIndex:0];
button.tag=ASCIINumber;
OUTPUT IS:- 65 for Upper case if lower case OUTPUT IS 97
But as using above method i suggest to you use simply set tag=intNumber instead of doing above.
It's not possible dear to set string in tag. Tag only takes int value as you said you know.
Create a custom view which is composed of UIButton and a NSString property or an ivar which will serve as tag.
#interface MyButton: UIView
#property(nonatomic, strong) UIButton *button;
#property(nonatomic, strong) NSString *uniqueTag;
#end
Hope that helps!
we can make a category for tag string in UIButton.
#interface UIButton (setTag)
#property(nonatomic, retain) NSString *tagString;
#end
And any where in your file you can set the tag string to your UIButton.
e.g
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTagString:#"Hello"];
you can always create a custom class containing a string property to go along with said classes
for example : create a custom class for UIButton
and then at the Header File put :
#property (nonatomic, strong)NSString *labelTag;
- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str;
and then, at the implementation create a super method of init with frame so it looked like this :
- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str{
self = [super initWithFrame:frame];
if (self) {
//Init code
_labelTag = str;
}
return self;
}
after that, whenever you want to set or call the string tag of the button you only need to declare it and call it like this :
MyCustomButton *button = [[MyCustomButton alloc]initWithFrame:frame withLabelTag:#"this is the title"];
//and to call it :
NSLog (#"the button's tag is :%#", button.labelTag)
or, you could always just use the button's title property and if you don't want it to show on your button, just set its HIDDEN prop to YES
There are two scenarios.
Case 1: If your tag string contains only numbers - (Eg: tagStringValue = "1433")
Use myButton.tag = [tagStringValue intValue];
Get back the button using
UIButton *myButton = ([[self.view viewWithTag:[tagStringValue intValue]] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:[tagStringValue intValue]]:nil;
Case 2: If your tag string contains numbers and strings - (Eg: tagStringValue = "ss1433kk")
Subclass the UIButton and add a property like:
#property (nonatomic, strong) NSString *stringTag; and use this instead of default Tag.

Linking stepper to Slider UIObject in C4

I am very new to C4 so please be gentle...
If I want to link a slider value to a label this is done with NSString stringWithFormat... e.g.:
self.mylabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
I added a stepper object as well, and now it also updates mylabel:
self.mylabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
But it would be intuitive if the slider position follows the label value when I'm using the stepper. but .value is not an available property in UILabel... so how do I take the mylabel.text property and push that into the slider.value property without getting a datatype mismatch error?
This question has 2 answers, how to do it using C4 objects and how to do it with Interface Builder / UIControls. I'll show both ways, UI first so that I can compare the C4 way afterwards.
UIControl
To do this with UIControls first set up your C4WorkSpace.h header so that it has the following methods and properties:
#property (assign, nonatomic) IBOutlet UILabel *myLabel;
#property (assign, nonatomic) IBOutlet UISlider *mySlider;
#property (assign, nonatomic) IBOutlet UIStepper *myStepper;
-(IBAction)sliderWasUpdated:(UISlider *)slider;
-(IBAction)stepperWasUpdated:(UIStepper *)stepper;
Then, in your drag all three components onto your projects XIB file (i.e. a UISlider, UILabel and UIStepper). Link the action sliderWasUpdated: to the slider using the valueChanged option, and the stepperWasUpdated: action to the stepper also using the valueChanged option. You do this step by selecting C4Canvas.xib from your project then right-clicking on the yellow cube, then dragging from the actions listed in the pop-up menu to each of the objects that you recently placed on the canvas.
Next, add the following code to your C4WorkSpace.m file:
#implementation C4WorkSpace
-(void)setup {
self.myStepper.minimumValue = 0.0f;
self.myStepper.maximumValue = 10.0f;
self.mySlider.minimumValue = 0.0f;
self.mySlider.maximumValue = 10.0f;
}
-(IBAction)sliderWasUpdated:(UISlider *)slider {
slider.value = [C4Math round:slider.value];
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
self.myStepper.value = slider.value;
[self.myLabel sizeToFit];
}
-(IBAction)stepperWasUpdated:(UIStepper *)stepper {
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
self.mySlider.value = stepper.value;
[self.myLabel sizeToFit];
}
#end
In the setup we make sure that the min/max values of both UI objects are the same (so that we can keep them matched up).
In the stepperWasChanged: method we do two things:
We use the stepper's value to set the label's text
We also use the stepper's value to set the slider's value!
In the sliderWasChanged: method we do the same thing, updating the stepper, but we also round the value of the slider so that it increments in steps (just to keep things tidy).
C4Control
To do the same with C4 objects, instead of native UI objects, we set things up a little differently. First, we don't add anything to our C4Canvas.xib, instead we'll set the objects up manually.
In your C4WorkSpace.h file, add the following lines of code:
#property (readwrite, nonatomic, strong) C4Label *myLabel;
#property (readwrite, nonatomic, strong) C4Slider *mySlider;
#property (readwrite, nonatomic, strong) C4Stepper *myStepper;
-(void)sliderWasUpdated:(C4Slider *)slider;
-(void)stepperWasUpdated:(C4Stepper *)stepper;
Notice that most of this is the same except we're using C4 instead of UI prefixes. Also, we call our methods -(void) instead of -(IBAction) because we're not using Interface Builder.
Next, add the following code to your C4WorkSpace.m:
#implementation C4WorkSpace
-(void)setup {
[self createAddObjects];
//calibrate the min/max values
self.myStepper.minimumValue = 0.0f;
self.myStepper.maximumValue = 10.0f;
self.mySlider.minimumValue = 0.0f;
self.mySlider.maximumValue = 10.0f;
}
-(void)sliderWasUpdated:(C4Slider *)slider {
slider.value = [C4Math round:slider.value];
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",slider.value];
self.myStepper.value = slider.value;
[self.myLabel sizeToFit];
}
-(void)stepperWasUpdated:(C4Stepper *)stepper {
self.myLabel.text = [NSString stringWithFormat:#"%4.2f",stepper.value];
self.mySlider.value = stepper.value;
[self.myLabel sizeToFit];
}
-(void)createAddObjects {
//set up the objects
self.myLabel = [C4Label labelWithText:#"values"];
self.myStepper = [C4Stepper stepper];
self.mySlider = [C4Slider slider:CGRectMake(0, 0, 192, 44)];
//position them
CGPoint centerPoint = CGPointMake(self.canvas.center.x,
self.canvas.center.y - 100);
self.myStepper.center = centerPoint;
centerPoint.y += 100;
self.myLabel.center = self.canvas.center;
centerPoint.y += 100;
self.mySlider.center = centerPoint;
//set up action bindings
[self.mySlider runMethod:#"sliderWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.myStepper runMethod:#"stepperWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.canvas addObjects:#[self.myStepper,self.myLabel,self.mySlider]];
}
#end
DIFFERENCES
The major difference between the two approaches is whether or not you use Interface Builder. In the C4 approach we need to add a method called createAddObjects to our project so that our slider, label and stepper all get added to the canvas.
This method also contains the methods for binding the actions of our C4UIElements to our code, which happens in the lines:
[self.mySlider runMethod:#"sliderWasUpdated:"
target:self
forEvent:VALUECHANGED];
[self.myStepper runMethod:#"stepperWasUpdated:"
target:self
forEvent:VALUECHANGED];
Once these are set up the only difference is specifying the use of C4 objects instead of UI objects, like:
-(void)sliderWasUpdated:(C4Slider *)slider {...}
instead of
-(IBAction)sliderWasUpdated:(UISlider *)slider {...}

Stepper value reset after loaded from coredate

I am building a UITabledetail view, which contains a stepper and a UILabel.
The uilabel will show the number of stepper pressed.
My problem comes when i used core data to save the value of the uilabel. e.g. the final value of the uilabel is 30.
When i load back the data, the uilabel showed 30 but, when i press the stepper again, the uilabel reset to 1 again.
Is there any way to make the stepper continue to count based on my saved value?
Below is my code.
- (IBAction)stepperValueChanged:(id)sender
{
double stepperValue = ourStepper.value;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}
- (IBAction)stepperValueChanged:(id)sender
{
NSString *tempString = [NSString stringWithFormat:#"30"];// you can pass here whatever data(stepper value) that you retrieve from core data...
double steppervalue = [tempString doubleValue];
double stepperValue = ourStepper.value+steppervalue;
self.label.text = [NSString stringWithFormat:#"%.f", stepperValue];
}
Hope, this will help you..

How do I program a slider to do two things with one button?

Basicly, I'm making an app that lets you add or subtract by a certain number and I want to do so with a slider. How would I program a slider to be able to add or subtract by an integer by tapping a different button for adding and subtracting. For example, say you want to add by 3. You slide the bar to three then tap the "+" to add three. How would I program this?
EDIT: This is the code that I want to implement it in:
int number = 0
-(IBAction)IncrementNumber:(id)sender {
number++;
[currentNumber setText:[NSString stringWithFormat:#"%d", number]];
}
-(IBAction)DecrementNumber:(id)sender {
number--;
[currentNumber setText:[NSString stringWithFormat:#"%d", number]];
}
#synthesize MySlider, MyTextField;
-(IBAction) sliderValueChanged:(UISlider *)sender {
MyTextField.text = [NSString stringWithFormat:#" %1.0f", [sender value]];
}
-(IBAction) changeButtonPressed:(id)sender {
NSString *textValue = [MyTextField text];
float value = [textValue floatValue];
if (value < 0) value = 1;
if (value > 100) value = 100;
MySlider.value = value;
MyTextField.text = [NSString stringWithFormat:#"%1.0f", value];
if ([MyTextField canResignFirstResponder]) [MyTextField resignFirstResponder];
}
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
if (MyTextField) {
if ([MyTextField canResignFirstResponder]) [MyTextField resignFirstResponder];
}
You could bind the slider to some variable, and the two buttons, [+] and [-] to two functions, say add and sub.
When the functions are called, they grab the value from the variable, and add / subtract it to / from something.
In your edit, assuming that sliderValueChanged is called when the slider value is changed, that function should alter number, so that IncrementNumber and DecrementNumber can have the updated value.
What should work is to define number as IBOutlet in your header file (.h). Then, in interface builder, you can bind the value of the slider to number. This way, whenever you change de slider, the number variable changes accordingly.
In the interface, declare two functions that will be bound to the [+] and [-] boutons:
IBOutlet int number;
-(IBAction) add: (id) sender;
-(IBAction) sub: (id) sender;
In the definition of those functions, you take whatever number you want to change and add to it the number variable that you declared earlier.
Something like the following should do the trick, if value has the number you want to modify:
-(IBAction) add: (id) sender {
value += number;
}
-(IBAction) sub: (id) sender {
value -= number;
}
You could of course combine the two functions into one, and decide wether you should add or subtract according to which button was pressed (you get that by looking into the sender argument) but I think it's less effective, and the code woud be more complex for no reason.
By using slider control u can set the value of that operands by it`s valueChanged event.After Setting value you can perform operation (+/-). for more help on slider you can find data here

iPhone OS: Using an NSString variable to specify a property or ivar

I have a program that I want to either hide or show certain UIbuttons depending on certain variables and all the buttons are named incrementally like 'button1, button2, button3.'
So I want to iterate through the buttons but I don't know how to address the button in an assignment statement using an nsstring as a variable inside a dot notation assignment, such as:
for (int i = 1; i < weekday; i++) {
int buttonIncrement = 0;
NSString *tempString = [[NSString alloc] initWithFormat:
#"calbutton%i", buttonIncrement];
self.tempString.hidden = YES;
}
The "tempString" part of the assignment I want to tell it to insert "calbuttonx" where x is the button number.
Can you do this somehow? if so please explain.
Use an array!
If you can't use an array, you can reference to a property by string with Key-Value Coding (KVC):
UIButton* button = [self valueForKey:tempString];
button.hidden = YES;
You can also assign a tag to each button in IB and get the button associated with the tag using
- (UIView *)viewWithTag:(NSInteger)tag
as defined on class UIView, for example:
for( int k = 0; k < 5; ++k ) {
id subview = [self.view viewWithTag: k];
if( subview ) {
...
}
}