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 {...}
Related
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.
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
I am using the reversesTitleShadowWhenHighlighted property on UIButton to reverse the title's text shadow(and it works great), but I really want the same thing for the selected state of the button(UIControlStateSelected).
Any answers to the related questions are also welcome:
Is there a way to do this with a UIButton without modifications?
Is there a way to set the shadowOffset of the title label per state with UIButton(similar to what is possible with the shadowColor)?
If you were to extend UIButton to add this, how would you do it?
I run into the same problem, so I guess it's no for your first two questions. Here is how I subclassed it :
#interface MyButton : UIButton
#property (nonatomic) BOOL reversesTitleShadowWhenSelected;
#end
#implementation MyButton
#synthesize reversesTitleShadowWhenSelected;
- (void)setSelected:(BOOL)selected
{
if (self.reversesTitleShadowWhenSelected)
{
if ((selected && !self.isSelected) ||
(!selected && self.isSelected))
{
CGSize offset = self.titleLabel.shadowOffset;
offset.width *= -1;
offset.height *= -1;
self.titleLabel.shadowOffset = offset;
}
}
[super setSelected:selected];
}
#end
I am trying to implement some form of snapping or steps with the UISlider. I have written the following code but it does not work as smooth as I hoped for. It works, but when the I slide it upwards it snap 5points to the right leaving the finger not centered over the "slide-circle"
This is my code where self.lastQuestionSliderValue is a property of the class which I have set to the initial value of the slider.
if (self.questionSlider.value > self.lastQuestionSliderValue) {
self.questionSlider.value += 5.0;
} else {
self.questionSlider.value -= 5.0;
}
self.lastQuestionSliderValue = (int)self.questionSlider.value;
It's actually considerably easier than I first thought. Originally I was trying to get the thumbrect property and do complicated math. Here's what I ended up with:
h File:
#property (nonatomic, retain) IBOutlet UISlider* questionSlider;
#property (nonatomic) float lastQuestionStep;
#property (nonatomic) float stepValue;
m File:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the step to whatever you want. Make sure the step value makes sense
// when compared to the min/max values for the slider. You could take this
// example a step further and instead use a variable for the number of
// steps you wanted.
self.stepValue = 25.0f;
// Set the initial value to prevent any weird inconsistencies.
self.lastQuestionStep = (self.questionSlider.value) / self.stepValue;
}
// This is the "valueChanged" method for the UISlider. Hook this up in
// Interface Builder.
-(IBAction)valueChanged:(id)sender {
// This determines which "step" the slider should be on. Here we're taking
// the current position of the slider and dividing by the `self.stepValue`
// to determine approximately which step we are on. Then we round to get to
// find which step we are closest to.
float newStep = roundf((questionSlider.value) / self.stepValue);
// Convert "steps" back to the context of the sliders values.
self.questionSlider.value = newStep * self.stepValue;
}
Make sure you hook up the method and the outlet for your UISlider view and you should be good to go.
The simplest solution to me was just
- (IBAction)sliderValueChanged:(id)sender {
UISlider *slider = sender;
slider.value = roundf(slider.value);
}
Another Swift approach is to do something like
let step: Float = 10
#IBAction func sliderValueChanged(sender: UISlider) {
let roundedValue = round(sender.value / step) * step
sender.value = roundedValue
// Do something else with the value
}
SWIFT VERSION
Example:
You want a slider to go from 1-10000 in steps of 100.
UISlider setup is as follows:
slider.maximumValue = 100
slider.minimumValue = 0
slider.continuous = true
In the action func() for the slider use:
var sliderValue:Int = Int(sender.value) * 100
Maybe someone will need!
In my situation I needed any integer step, so I used the following code:
-(void)valueChanged:(id)sender {
UISlider *slider = sender;
slider.value = (int)slider.value;
}
A really simple one:
- (void)sliderUpdated:(UISlider*)sli {
CGFloat steps = 5;
sli.value = roundf(sli.value/sli.maximumValue*steps)*sli.maximumValue/steps;
}
Great if you want a fast solution and you've added the target by UIControlEventValueChanged.
Hey guys... today is my first day hacking away on the iphone SDK. Having a blast but have a quick question.
I'm trying to move an UIView around the screen dynamically by sending it information from a slider. My slider is working fine but I cant seem to figure out how to get the UIView to move.
My .h file...
#interface Slider_BallViewController : UIViewController
{
IBOutlet UISlider *theslider;
IBOutlet UITextField *ytext;
IBOutlet UIView *theball;
}
- (IBAction)moveVert:(id)sender;
My .m file...
- (IBAction)moveVert:(id)sender
{
int progressAsInt = (int)(theslider.value);
NSString *newText = [[NSString alloc] initWithFormat:#"%d", progressAsInt];
ytext.text = newText;
theball.frame.origin.y += progressAsInt;
}
I get an error on the frame.origin line in my .m file that says... lvalue required as left operand assignment. Not sure what im doing wrong.
Any help is great, thanks.
If you want to modify a UIView's frame property, you should do it by following:
CGRect curFrame = theball.frame;
curFrame.origin.y += progressAsInt;
theball.frame = curFrame;