How to set value of UISlider from a UITextField - xcode

I am using Xcode 4.3 and I want to be able to edit the value of a UISlider using a UITextField. How can I do this? I have a slider that displays its value in a text field, but can I also set the slider's value from a text field?
Thanks for the help!!

Assuming "textField" is the name of your text field, I would add a target to it in your viewDidLoad method:
[textField addTarget:self
action:#selector(textFieldDidChange)
forControlEvents:UIControlEventEditingChanged];
And then handle the change like you said:
- (void)textFieldDidChange {
Slider.value = [[textfield.text] intValue];
}
Hope this helps.

Related

Replace UITextField Placeholder

I have a 'IBOutlet UITextField* textField' connect with a UITextField in a .xib file.
I use this element as a combobox in a viewcontroller class which is connect to a VC in the storyboard.
My question is how can i modify the placeholder text? I already try the 3 following methods, but nothing work!!
-(void) setPlaceholder:(NSString *)label
{
textField.placeholder = label;
[textField setPlaceholder:label];
[textField cell ]setPlaceholderString:label];
}
thx for your answers
put it in the viewdidload method of your viewcontroller method because the placeholder field wasn't initialize when i called my setPlaceholder method
As long as you're textField has been synthesised, you should be able to use:
textField.placeholder = label
textField.placeholder = #"placeholder" shall work, check if label and textField actually point to respective objects.

Setting text on multiple UILabels in a NSArray collection

I am trying to set multiple labels with identical text and cannot for the life of me figure out the proper way of doing so.
I am using an ibaction to handle a switch that will either change several labels in a collection to say "yes" or "no" and have been trying both a for loop and makeobjectsperformselector withobject method, but so far no luck.
Any insight would be greatly appreciated.
You can set the tag of UILabel subviews to help you out with this. If it's not already set, go to the storyboard, click on your label, go to the Attributes Inspector and under "View" there's a tag field.
If the labels all have different tags (0,1,2...) , the following loop should do what you need:
for(UIView *subview in [self.view subviews] ) {
if([subview isKindOfClass:[UILabel class]]) {
UILabel *currentLabel = (UILabel *)[self.view viewWithTag:subview.tag];
currentLabel.text = #"yes";
}
}

how to access multiple UITextView in table view cells

I have a table view with several cells, each containing a UITextView.
The user can edit each text view that he clicks.
But when he clicks DONE, how do I access each text view, to read each that was edited?
You can use UItextField delegate to get the text from current text filed as -
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *textFieldValue = [textField text];
}
Let's say your UITextView is called "tv"
You can use something like:
NSString *contents = tv.text;
I believe you can also use something along the lines of:
NSString *contents = [tv getText];

setting an NSTextView

How can I set an NSTextView to an NSAttributedString? I do not want to use a textConainer, and I tried:
[myTextView insertText:myMutableAttributedString];
but it didn't work. Nothing happened...not even a warning or a runtime error. Any help would be appreciated.
Textview must be editable to insert.
[myTextView setEditable:YES];
[myTextView insertText:myMutableAttributedString];
[myTextView setEditable:NO];
In Swift 2.2:
myTextView.textStorage?.setAttributedString(myMutableAttributedString)
In Objective-C:
[[myTextView textStorage] setAttributedString:myMutableAttributedString];
You should call -setString with an empty string before calling that method (otherwise you're inserting text into a null string object):
[myTextView setString:#""];
[myTextView insertText:myMutableAttributedString];
the textView is createed programmatically? if you load the NSTextView from xib,it will bi fine.

Disable/Enable NSButton if NSTextfield is empty or not

I´m newbie with cocoa. I have a button and a textField in my app. I want the button disabled when the textfield is empty and enabled when the user type something.
Any point to start? Any "magic" binding in Interface Builder?
Thanks
[EDITED]
I´ve tried to set the appDelegate as the NSTextfield´s delegate and added this method (myTextfield and myButton are IBOutlets):
- (void)textDidChange:(NSNotification *)aNotification
{
if ([[myTextField stringValue]length]>0) {
[myButton setEnabled: YES];
}
else {
[myButton setEnabled: NO];
}
}
But nothing happens...
I´ve tried to set the appDelegate as the NSTextfield´s delegate and added this method (myTextfield and myButton are IBOutlets):
- (void)textDidChange:(NSNotification *)aNotification
{
if ([[myTextField stringValue]length]>0) {
[myButton setEnabled: YES];
}
else {
[myButton setEnabled: NO];
}
}
That's the hard way, but it should work just fine. Either you haven't hooked up the text field's delegate outlet to this object, you haven't hooked up the myTextField outlet to the text field, or you haven't hooked up the myButton outlet to the button.
The other way would be to give the controller a property exposing the string value, bind the text field's value binding to this stringValue property, and bind the button's enabled binding to the controller's stringValue.length.
You could also give the controller two properties, one having a Boolean value, and set that one up as dependent upon the string property, and bind the button to that. That's a cleaner and possibly more robust solution, though it is more work.
Here's a solution using bindings.
Below I setup a NSTextField that is bound to the file owner's "text" property. "text" is a NSString. I was caught by "Continuously Updates Value". Thinking my solution didn't work but really it wasn't updating as the user typed, and only when the textfield lost focus.
And now setting up bindings on the button, simply set its enabled state to the length of the file owner's text property.
Annd, the working product.
If you use controlTextDidChange instead of textDidChange, you can get rid of the notification stuff and just rely on being the NSTextField's delegate.
Thanks Peter. What I missed (in my hard way version) is this piece of code in the awakeFromNib in the appDelegate:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(textDidChange:) name:NSControlTextDidChangeNotification object:myTextField];
It works perfect. Now I´m trying the easy way, but I´m afraid I´m not still good enough with the bindings.
To bind the property
#property (retain) IBOutlet NSString *aStringValue;
with the textfield´s value, what I have to use in IB for "Bind to:", "Controller Key" and "Model Key Path"?

Resources