setting a default value for slider when app loads - iphone

In my iPhone app I'm using a slider for adjusting the volume and working fine.
But I'm not able to set a default value for slider when app loads.
I have seen that only when the slider is touched the value gets changed.But i need a default value when app loads.
My code is given below
- (IBAction)sliderChanged:(id)sender {
slider = (UISlider *) sender;
slider.minimumValue = 0.5;
slider.maximumValue = 2.2;
progressAsInt = slider.value ;
}
how can i solve this issue.
please help.Thanks in advance.

There are two ways to change default value of slider.
Using Interface builder. See attached picture and value of current property.
Using Code
You just need to write this one line in viewWillAppear delegate method.
-(void)viewWillAppear:(BOOL)animated{
slider.value = 1.0;
}
Hope this help.

First you'll need to set the slider to an IBOutlet iVar of the view controller where the slider is.
Then, in the viewDidLoad of that view controller, you can set the value you want.
(Also, you should be able to do it in Interface Builder, but I'm not very familiar with it).

Related

get the frame of a view made in IB in viewDidLoad?

When I create a label in Interface Builder and hook the outlet to a viewController, and log the label in viewDidLoad as follows:
NSLog(#"label: %#",self.label);
It gives me a frame of (0,0,0,0). self.view gives me a size over 0. An older program's label also gives me a size over 0. It will log the text in the label, so it is hooked up right. Is this a change in Xcode 4.5? How do I access the frames that I set in Interface Builder?
this is the log:
UILabel: 0x754e1b0; frame = (0 0; 0 0); text = 'this is a label'; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; userInteractionEnabled = NO; layer =...
Usually the frame layouts are unreliable in viewDidLoad. In the case of a nib this shouldn't be a problem. Are you using AutoLayout by any chance ?
Please also log the same values in viewWillAppear, this will certainly have the correct values. I usually use viewWillLoad to initiate my UI elements, and viewWillLoad to place them correctly.
Try this:
NSLog(#"Label Frame: %#",NSStringFromCGRect(self.label.frame));

How to release user touch on UISlider?

I have such a problem... I want to block user's slider when he is sliding. I thought .isUserInteractionEnabled will handle that but it works only after the user releases slider.
I would like to 'steal' the slider from user and stop it.
How to do that?
You should add a selector to your UISlider object. For instance, if you had a UISlider named "slider" you should do this:
[slider addTarget:self action:#selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
//runs a method called "sliderMoved" located in the same class as the slider (hence the addTarget:self) when the value is changed of the slider
and then you could make a method like this, which sets the value at the average whenever the user tries to slide the slider:
-(void)sliderMoved:(UISlider*)sender{
sender.value = (sender.maximumValue + sender.minimumValue) / 2;
}

Custom UIButton, enabled property works but hidden property won't

I have a little problem on my Xcode project (iPhone app).
I just modified the rounded rect UIButton I used before to a custom one but now the hidden property isn't working anymore.
if (artwork)
{
artworkImage = [artwork imageWithSize: CGSizeMake (256, 256)];
shareButton.alpha = 1.0;
shareButton.enabled = YES;
//[shareButton setHidden:NO]; // Won't work, I don't know why
}
else
{
shareButton.alpha = 0.0;
shareButton.enabled = NO;
//[shareButton setHidden:YES]; // Won't work, I don't know why
}
I found a workaround, using enabled property and alpha instead of hidden.
But I would like to understand why the hidden property isn't working anymore.
Thanks for the answers.
Are you trying to animate the show/hide? I believe the hidden property is not animatable. See this question. A workaround in this case is to use the alpha property use the animation callback ([UIView setAnimationDidStopSelector:]) to set the visible state after the animation has been completed.
Try shareButton.hidden=YES;

UISlider highlited property for showing the value.

i have IBOutlet uislider. i want it to show the value of slider when the user presses the slider and change the value of it when the user take his hand off the slider i want it to disapper. So when the user touches to change the value of slider the label shows the value and when the user take his finger of the slider the label automaticly disapper.
My code is:
-(IBAction)sliderSlide:(UISlider *)aSlider {
float f=slider.value;
NSString *show=[NSString stringWithFormat:#"%.2f %%",f];
label2.text=show;
}
i know i need to use slider.highlited=YES; but where and how can i turn it back to hidden?
- (IBAction)touchEndedAction
{
self.label2.hidden = YES;
}
set the IBAction to the sliders UIControlEventEditingDidEnd or UIControlEventTouchCancel
try it out.
UIControlEventEditingDidEnd didn't work for me, but UIControlEventTouchDown works

Menu over slider - iPhone getting the x co-ordinate of UISlider Pointer when sliding

It is ok to set the max & min value for a UISlider control in iPhone. Accordingly slider will work - OK - Good.
First, I will give an example of what exactly I want to implement.
In iPad comes with Calendar application as a default.
Open calendar, at the bottom - you can see day/week/month listing
When you slide your finger on it, you will see a pop up menu when sliding.
I want to implement the same functionality on slider.
I am trying hard, But if I am able to get x co-ordinate, I can easily implement it. Any other suggestion for implementing same are most most welcome.
Start out with a UILabel for testing (just write something in it) and add IBOulets to it, as well as the UISlider. Next, add a IBAction for "value changed" for the slider.
You then need to calculate two values to figure out where your label should be placed -- adjustment from left, and pixels per value for the slider. Your #interface might look like this:
#interface sliderViewController : UIViewController {
IBOutlet UISlider *slider;
IBOutlet UILabel *label;
float pixelsPerValue;
float leftAdjust;
}
-(IBAction) sliderValueChanged:(id)sender;
#end
In e.g. viewDidLoad: for your view controller, you do the calculation for the two floats.
- (void)viewDidLoad {
float width = slider.frame.size.width;
pixelsPerValue = width / (slider.maximumValue - slider.minimumValue);
leftAdjust = slider.frame.origin.x;
[super viewDidLoad];
}
And finally, your IBAction might look like this:
-(IBAction) sliderValueChanged:(id)sender
{
NSLog(#"changed");
CGRect frame = label.frame;
frame.origin.x = leftAdjust + (pixelsPerValue * slider.value);
label.frame = frame;
}
Hope that helps.
I don't think that thing is implemented with a UISlider, but you can use the x-coordinate of the touch easily.
In the UISlider's action, you could accept a 2nd argument.
-(void)action:(UISlider*)sender forEvent:(UIEvent*)event
// ^^^^^^^^^^^^^^^^^^^^^^^^
From the UIEvent you can get a UITouch which contains its x, y coordinates.