Replace UITextField Placeholder - xcode

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.

Related

Enabled binding for NSTextField bound to NSString

#interface MyClass {
NSString *_myString;
}
#property (copy) *myString;
#end
#implementation MyClass
#synthesize myString = _myString;
- (void)awakeFromNib {
self.myString = #"";
}
In the nib, I have an NSTextField and an NSButton. The text field's Value binding is set to myClass.myString. I verified that the variable _myString is being updated correctly when text is typed into the text field.
The Enabled binding of the NSButton is set to myClass.myString.length. However, when I start up the program, the NSButton is enabled! If I go to the textfield and type something into it, the button stays enabled. Then if I erase the text from the textfield, the button becomes disabled.
But why isn't the button disabled to begin with, after the call in awakeFromNib ? Do I have to do some additional work to make the binding work in the opposite direction (myClass.myString --> NSTextField) ? I thought that declaring myString as a property would do the trick.
I'm not sure what is causing this but I would say the easiest way to fix it, since your text will start out empty, is in awakeFromNib do something like [myButton setEnabled:false];

How to set value of UISlider from a UITextField

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.

iOS Getting the value from a TextView in xCode

I am trying to write a master detail application and I have a TextView and whatever the user enters in the TextView, becomes the new item. But whenever I press the add button it crashes the program with error: object cannot be nil
Here is my code:
#interface MasterViewController () {
NSMuteableArray *_objects;
IBOutlet UITextView *text;
}
And at Insert New Object method I have this:
[_objects insertObject:text.text atIndex:0];
Do I need to hook it up to my actual TextView somehow?
Any help would be amazing, thank you!
Two things:
Make sure you actually wired that outlet up to the TextView
Make sure you allocated that NSMutableArray somewhere
_objects = [[MSMutableArray alloc] init];

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];

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