How do I place a slider value in label? - ios

I am quite new to Xcode and I am having trouble placing the value of a slider in a label when the slider is value is changed.
Here is my code:
Header file:
#interface ViewTwoViewController : UIViewController
{
IBOutlet UISlider *slider;
IBOutlet UILabel *sliderLabel;
}
-(IBAction)sliderValue:(id)sender;
and here is my m file:
-(IBAction)sliderValue:(UISlider *)sender {
sliderLabel.text = [NSString stringWithFormat:#"%g", slider.value];
}
I am not 100% sure why the value isn't updating?
I am looking forward to hearing from you all!

Here's an XCode project that does what you're looking for: http://clrk.it/013r203C1y1F
Note that:
The label and slider are hooked up to the UIViewController as
IBOutlets in the storyboard.
The slider's valueChanged: method is linked to the UIViewController in the storyboard.
The slider's value is displayed using %f.
Your method would look something like this:
-(IBAction)sliderValueChanged:(id)sender
{
if (sender == _slider) {
_label.text = [NSString stringWithFormat:#"%0.3f", _slider.value];
}
}

Here would be my process to figure out the root cause of this problem.
Did you remember to connect the slider and sliderLabel outlets?
Did you remember to connect the IBAction to the slider? Set a breakpoint. When you drag the slider, does sliderValue: get called? What are the values of slider and sliderLabel?
Its unusual that your IBOutlets are not properties. I would expect to see:
#property (weak, nonatomic) IBOutlet UISlider *slider; and
#property (weak, nonatomic) IBOutlet UILabel *sliderLabel;

I tried your code and seems to be correct..Please check these things:-
1) Have you connected Slider action Value Changed to IBAction method for proper functioning.
2) Check our connections with Outlets.
Hope helps you out..

Related

Need to connect a UISlider created with Interface Builder to non-event code

I have a ViewController that populates a couple of sliders as so:
// ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISlider *aSlider;
#property (weak, nonatomic) IBOutlet UISlider *anotherSlider;
#end
// ViewController.m
#interface ViewController ()
#end
#implementation ViewController
-(void)resetSliders
{
NSLog(#"Resetting sliders sl1: %# sl2: %#", self.aSlider, self.anotherSlider);
[self.aSlider setValue:0.5f animated:NO];
[self.anotherSlider setValue:0.5f animated:NO];
}
- (IBAction)setOneSlider:(UISlider *)sender {
NSLog(#"Setting aSlider to %f", sender.value);
self.sl1Val = sender.value;
[self doAThing];
}
- (IBAction)setTwoSlider:(UISlider *)sender {
NSLog(#"Setting anotherSlider to %f", sender.value);
self.sl2Val = sender.value;
[self doBThing];
}
#end
The calls to setOneSlider and setTwoSlider are wired to the value changed events of the two sliders. That works perfectly well. Unfortunately, I have not connected the sliders to their programmatic handles, because the call to resetSliders yields no change in the UI, even with a call added to setNeedsDisplay on the two slider elements.
When resetSliders is called, the log shows:
2015-04-28 12:22:19.608 myApp[4489:681127] Resetting sliders sl1: (null) sl2: (null)
Can anyone indicate to me how to set the self.aSlider and self.anotherSlider with the correct object handles?
To add some color: I have dragged the UI representation of the sliders to their respective #property declarations, and I can see that they have filled-in grey circles in the margin, indicating that they've been wired. The Connections Inspector shows both a "Value Changed" event trigger and a Referencing Outlet connecting myApp to the slider(s).
I'm extremely new to IOS programming and am reaching the limits of learning-by-failing.
The properties should be defined inside the #interface blocks. Yours are currently outside. Additionally, they should probably be weak instead of retain, and no need for the #synthesize statements or the ivars.
Lastly, when you reference them in code, you should do so as self.aSlider, not just aSlider.
It sounds like you still need to connect these outlets in Interface Builder:
#property (retain, nonatomic) IBOutlet UISlider *aSlider;
#property (retain, nonatomic) IBOutlet UISlider *anotherSlider;
Also you don't need to declare properties and ivars for the same sliders. Just declare and use the properties you already have.
#interface ViewController : UIViewController
and
-(void)resetSliders
{
// note the addition of 'self.' below
NSLog(#"Resetting sliders sl1: %# sl2: %#", self.aSlider, self.anotherSlider);
[self.aSlider setTo:0.5f animated:NO];
[self.anotherSlider setTo:0.5f animated:NO];
}

UITextField Alignment issue iOS 7

I am stuck in a very strange problem. A default text field in iOS 8 & 7 behave very strange.
If the text box got focused the alignment of text get changed check the screen shots.
even with very small text size
Edit:
#import <UIKit/UIKit.h>
#interface LoginForm : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *txt_username;
#property (weak, nonatomic) IBOutlet UITextField *txt_password;
- (IBAction)loginButtonPressed:(id)sender;
#end
Try this. Its more of a work around, not exactly getting to the root of the problem but make your VC a UITextFieldDelegate, then make it the textFields delegate and use the following code. -
(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Change the alignment if you need to.
textField.textAlignment = NSTextAlignmentCenter;
}
It should be a alignment issue. In your storyboard check the UITextField Alignment matches with the below image.

how to make text appear on multiple views with one label?

I'm using a scroll view with 3 views, and I need to place one label that shows up on each view,
the views are on separate view controllers: in view controller.h
#import "PagerViewController.h"
#interface ViewController : PagerViewController {
}
#property (strong, nonatomic) IBOutlet UIView *View1;
#property (strong, nonatomic) IBOutlet UIView *View2;
#property (strong, nonatomic) IBOutlet UIView *View3;
I've placed them as (in ViewController.m)
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View1"]];
Should I fix an NSString
and Outlet to do this?
I need the labels to move with the scroll not to be separate from each other.
What you want, cannot be done with a single UILabel. You will have to create 3 separate labels and write the logic that will update all of them.
Create the UILabel outlets and write a method something similar to the method below:
- (void)updateLabels:(NSString *)text
{
self.label1.text = text;
self.label2.text = text;
self.label3.text = text;
}
This is just the way UIView's work and you cannot make them draw outside of their bounds.
I suggest you follow up on more iOS basics before going into complex layouts.
If you need to separate these views, a well-maintainable solution could be three separated label and one function (updateLabels:) in which the three label is updated to the same value.

Can't create IBOutlet for UIBarButtonItem in a UIViewController

I'm trying to create an IBoutlet from a UIBarButtonItem to a UIViewController, but when ctrl+clicking and dragging to the controller, I see no indicator whatsoever, and thus it's impossible to create it. Not sure what I'm doing wrong.
Here's the screenshot, in case it could clear something up:
Thanks a lot in advance!
EDIT: I also tried to create the IBOutlet manually with #property (weak,nonatomic) IBOutlet UIBarButtonItem *barButtonItem; and then ctrl+clicking and dragging, but same result.
I was able to create a UIBarButtonItem IBOutlet
#property (nonatomic, weak) IBOutlet UIBarButtonItem *cameraLibraryToggleButton;
if u used story board then select UIBarButtonItem item and just put on the view controller.then, set IBOutlet. try this
I do not think you can create an IBOutlet for a UIBarButtonItem... Instead create a IBOutlet for your UIToolbar and give your UIBarButtonItem a tag of 1. Next use this code:
for(UIBarButtonItem *button in self.toolBar.items)
{
if(button.tag == 1)
{
UIBarButtonItem *newButton = button; //Your UIBarButton
//Do whatever you want with it.
}
}
Make Sure you have assign class (e.g .UIViewController) to your storyBoard

can not move buttons

I have a little problem that my buttons doesn't move when I am using a code that should move the button.
ViewControlle.h:
#property (strong, nonatomic) IBOutlet UIButton *button1;
#property (strong, nonatomic) IBOutlet UIButton *button2;
ViewController.m:
#implementation GameViewController
#synthesize button1,button2;
code which should move the buttons:
button1.center=CGPointMake(button1.center.x, button1.center.y-10);
button2.center=CGPointMake(button2.center.x, button2.center.y+10);
why does the code not work or am I doing something wrong?
Because autolayout did not happen before view did load it will reset your locations.
when - (void)viewDidLayoutSubviews gets called its done so place your code here.
If you are using AutoLayout for ViewController than It's not work.
Uncheck the AutoLayout for ViewController.

Resources