UITextField and UIButton - ios

There is a textField and a button in my view, and if the
textfield is empty I do not want to user can click the button.
When user text something in the textfield, the button will bu
clickable.
How can I do this? Thank you.

for your UITextField, you can set:
[textField addTarget:self action:#selector(editing:) forControlEvents:UIControlEventAllEditingEvents];
Then in your editing: have:
-(void)editing:(UITextField *)sender {
myButton.enabled = ![sender.text isEqualToString:#""];
}

Listen for changes to the text field. As the text changes, update the button's enabled property based on whether there is text or not. Of course you also need to set the button's state at the beginning as well.
// Setup the text field change listener (this can be done in IB if appropriate)
// Put this in viewDidLoad if not using IB.
UITextField *myTextField = ... // a reference to the text field
[myTextField addTarget:self action:#selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged];
// Initialize the button's state (put this in viewDidLoad)
myButton.enabled = myTextField.text.length > 0;
// The method called as the text changes in the text field
- (void)textFieldChangedAction:(UITextField *)sender {
myButton.enabled = sender.text.length > 0;
}

If you want, you can "hide" the button by setting the alpha value of the button to 0 and when the textfield is at least one character long, then set the button's alpha value to 1 to "show" the button. I think this conceptually is easy to do and very presentable to the user.

Set the textfield.delegate to self
Then in delegate that is textfielddidbeginediting
(
Check is textfield is #""
Then disable the button
Else
Enable the button
)
And there is one more. Delegate u can use
Textdidchangecharacters
And in viewdidload first state of the button should be disabled

Related

pickerview hidden when scrollview userInteraction Disabled

My functionality is to open picker on click of textField. My textfield is inside scrollview is shown in below image.
I want to disable userinteraction of scrollview when picker opens. Following is my code.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.picker removeFromSuperview];
[self.toolBar removeFromSuperview];
textField.inputView = self.picker;
textField.inputAccessoryView = self.toolBar;
self.scrollview.userInterationEnabled = NO;
return YES;
}
When I comment the userInteractionEnabled code. Picker is opening perfectly. But when I uncomment code picker is not opened.
Also I gave some delay for this code. so after dalay my picker is hidden again.
The problem here is that userInteractionEnabled is inhereted from the scrollView to the textField. And as it is explained here
A UITextField will also refuse to become first responder if its userInteractionEnabled property is NO as I just discovered. I had to explicitly re-enable user interaction on the text field before it would accept first responder status.
And it can not show the inputView of the textField.
You shou ensure that your textField userInteracationEnables is true or to move the pickerView outside of the textField.inputView.
You can use this cool customizable control for having picker as an inputView on textfield.
Or this one if you want to disable the interaction to background view while picker is open.

Rightview on UITextField does not disappear on editing

I have a rightView on UITextField with the mode UITextFieldViewModeUnlessEditing, so it should disappear when we are editing the textfield.
But now when I start editing the textfield programatically when you press the rightView (an edit button) the rightView does not disappear immediately. It only disappears when I start typing something.
So why does editing for a UITextField only start on typing and not on becoming first responder? How do I fix this?
Add the < UITextFieldDelegate > to your interface.
Then set the delegate or your textfield to self (either in IB or) for example in viewDidLoad:
self.theTextField.delegate = self;
Add this method to your .m :
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// Whatever is supposed to happen when you begin to edit the textfield happens now
}

How to set keyboard as inputview in UItextfiled

I have one UItextfiled and in viewdidload i set his input view property as nil like this.
textField.inputView = nil
In in my view i have one button when i click on the button i have to set the textfield's inputview property as keyboard how can i do this.please help me.
I'm not exactly sure what you want to do, but probably you want your textField to became active, with a keyboard. That is done with
[textField becameFirstResponder];
and other way around is
[textField resignFirstResponder];

Set text inputed from keypad on UILabel on UIButton click

i want to write the text to UILabel which is inputed from iphone's keypad. when i tapped button as in screen shot. how i can do that ? i don't want to use Textfield.
-(IBAction) buttonTapped {
[self.yourlabel setText:[textField text]];
}
Add a UITextField (you can name it hiddenTextField) some where, and set it hidden in view, so it'll become invisible to everyone, in viewDidLoad method of that UIViewController write,
[hiddenTextField becomeFirstResponder]; //it will make UIKeyBoard show on screen
On your UIButton action write,
- (IBAction)myAction {
myLabel.text = hiddenTextField.text;
//Don't forget to set `hiddenTextField` delegate to self.
[hiddenTextField resignFirstResponder];
}

how to make the textfiled clear on click

i have app i want that when user enters any data and text field has by default 10 so when user click on textfield then it clear the textfield to enter data.It mean when user enters data it doest not delete one bye one like clear 1 and o with back space but by default when user click textfield it should get clear.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textfield settext:#""];
}
if you focus on textfield then this method is called and textfield is cleared.
Apple Developers already did this for you, set the 10 into UITextField placeholder property see doc, when you tap on the textfield, it will be remove automatically!
However, if you want to clear it in case you've added 10 as text of textfield then,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = #"";
}
It will call each time when you focus to textfield.

Resources