How to programmatically deselect UITextField - ios

I have a UITextField which I can click on, and edit the contents of. I am running code when the UITextField is pressed by using:
[personalCountryLabel addTarget:self action:#selector(countryPressed) forControlEvents:UIControlEventEditingDidBegin];
This presents another view controller. However, when I click the back button, the UITextField is still selected, so the text runs again, sending me back to the view controller.
I use the code:
textField.enabled = false;
and
textField.enabled = true;
to respectively turn off and on the editing of the UITextField, but doing this in succession does not unselect the UITextField.
How can I therefore programmatically deselect the UITextField (i.e, where the line cursor is no longer blinking).

If I understand what you're asking correctly, you just want:
[textField resignFirstResponder];

/* Programmatically deselect the Uitextfiels below this Code */
UITextField *txtDeselect = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.origin.x+20, self.view.frame.origin.y+20, self.view.frame.size.width-40, 40)];
[txtDeselect setBackgroundColor:[UIColor yellowColor]];
[txtDeselect setBorderStyle:UITextBorderStyleRoundedRect];
[txtDeselect setTextAlignment:NSTextAlignmentCenter];
[txtDeselect setText:#"Email"];
txtDeselect.enabled = NO;
[self.view addSubview:txtDeselect];

did you set outlet & delegate of UITextField in your view controller ?
why you use UItextfield for this ?
i recommend you use one of this before presenting new view controller :
option 1 :
[youtextfield resignFirstResponder];
//please sure you outlet connected & ....
you can call this on your viewWillAppear
option 2 :
[self.view endEditing:YES]; // this regularly
happend after present another VC)
you shouldn't use shouldEndEditing

Related

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

Dismiss Keyboard

So I have a self created top bar controller that is being implemented in my other controllers views. I have a textfield on this top bar. I was wondering what the best approach to having the keyboard dismiss if the user clicks anywere outside the keyboard. I do have a tap gesture recognizer that performs the method dismisskeyboard. However, this only works if the user clicks on the top bar outside the keyboard. Is there a way to set it up so if the user clicks anywere on the screen, then this will dismiss the keyboard?
The approach I would describe is a hack but still works.
create a transparent UIButton with the frame of the view, like below:
UIButton* overlay = [UIButton buttonWithType:UIButtonTypeCustom];
overlay.frame = self.view.bounds;
overlay.backgroundColor = [UIColor clearColor];
[overlay addTarget:self action:#selector(hideOverlay:) forControlEvents:UIControlEventTouchDown];
[self.view.subviews[0] insertSubview:overlay belowSubview:self.textField];
Create a method hideOverlay to dismiss the keyboard and hide the transparent:
-(void)hideOverlay:(id)sender {
UIView* overlay = sender;
[overlay removeFromSuperview];
[self.textField resignFirstResponder];
}
You should ideally call the first block of code in textFieldDidBeginEditing: protocol method of UITextFieldDelegate and you should register your calling class accordingly.
You might try giving the text field a transparent inputAccessoryView, sized to fill the rest of the screen, that catches taps and dismisses the keyboard.

UITextField in inputAccessoryView won't becomeFirstResponder

I have a view called songInfoView with 3 UITextFields and a UIButton. I create a temp UITextField in the parent view to bring up the keyboard and assign songInfoView as the inputAccessoryView. This works as expected.
Then, I try to set a text field in songInfoView as first responder. The cursor moves to this field, but when I try to type, nothing happens. I can tap on the text field and type as expected, but I want it to work without having to tap on it. Am I missing something? Am I doing this out of order? Thanks for your help.
songInfoView = [[SongInfoViewController alloc]init];
songInfoView.delegate = self;
UITextField *tempTextField = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 30.0f)];
[self.view addSubview:tempTextField];
[tempTextField setInputAccessoryView:songInfoView.view];
[tempTextField becomeFirstResponder];
[songInfoView.titleTextField becomeFirstResponder];
Well, I figured it out myself. It turns out (I think) that the on screen keyboard (along with it's accessory view) has it's own window. So, within songInfoView's viewDidAppear method, I had to call
[self.view.window makeKeyAndVisible];
Just remember to call this again within the view controller you used to load the keyboard after the keyboard disappears. Hope this helps someone else down the road.

Using a UIButton inside a UITextField as a clear button

what i am trying to do is do a custom clear button in a UITextField, currently i have got everything working except for the last part. Which is getting it to preform the clear action. However, i have not been able to work out how i can get it to clear, just the textField it is in.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField setTextColor:[UIColor colorWithRed:0.56f green:0.56f blue:0.56f alpha:1.00f]];
[textField setFont:[UIFont systemFontOfSize:14]];
textField.background = [UIImage imageNamed:#"whiteCell"];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(0, 0, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"clearBut"] forState:UIControlStateNormal];
textField.rightView = btnColor;
textField.rightViewMode = UITextFieldViewModeAlways;
[textField addSubview:btnColor];
return YES;
}
This is how i've created the button in the textField, and for it to only show when editing, as you can see i have it calling clearText however i'm unsure how i can send the current textField name i am in to be cleared in the clearText call.
I know I can do it the hard way, and define it individually for each of my textFields, but i'm sure there is an easier way to go about this, that i just haven't realized.
I suggest subclassing UITextField. This will not allow you to lay out the button in a Storyboard, but in Code or as a .nib file should work. Of course you can use the UITextfield subclass in a storyboard.
Inside the subclass add a UIButton as a subview and in its action method call:
self.text = #""
Let me know if you have any further questions.
Given that your button has been added to the text field as a sub-view you can get to the text field in the buttons superview property:
- (void) clearText:(UIButton*)sender
{
UITextField* textField = (UITextField*)sender.superview;
textField.text = nil;
}

Adding [myUITextField becomeFirstResponder]; does not bring up keyboard

I've created a screen that has a UITextField on it. When I get a EditingDidBegin event, I resignFirstResponder, Bring up a Popover with another textField in it and for that TextField call the BecomeFirstResponder on it.
When it runs, I get Blinking insertion pointer and the X clear contents. Though no Keyboard. The Master UIView is set to UserInteractionEnabled:YES.
target action for First UITextField, its on its own view.
[textField addTarget:self action:#selector(wantsToEditValue:) forControlEvents:UIControlEventEditingDidBegin];
Target Action selector:
- (IBAction)wantsToEditValue:(id)sender {
// set notification so we can update from popover
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(saWriteValue:)
name:kRefreshFromPopover object:nil];
//we dont want the TagValue textfield to really be the first responder.
[textField resignFirstResponder];
[... setup popoverVC, and View. present popover...]
}
Here is the code to create the 2nd UITextField. This code is in the VC for the Popover..
- (void)viewDidLoad
{
if (IoUIDebug & IoUIDebugSelectorNames) {
NSLog(#"%# - %#", [self description], NSStringFromSelector(_cmd) );
} [super viewDidLoad];
[self createElementInputControl];
[self createWriteButton];
//We want the input Focus
[textFieldInput becomeFirstResponder];
//Resize our view to handle the new width
CGRect newViewSize = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
writeButton.frame.origin.x + writeButton.frame.size.width + kWriteElementOffset ,
self.view.frame.size.height);
[self.view setFrame:newViewSize];
}
Create Input Code:
-(void) createElementInputControl {
textFieldInput = [[UITextField alloc] initWithFrame:CGRectMake( kWriteElementOffset ,
kWriteElementHeightOffset,
kTagValueInputInitialWidth,
kWriteElementDefaultHeight)];
textFieldInput.borderStyle = UITextBorderStyleRoundedRect;
textFieldInput.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldInput.textAlignment = UITextAlignmentLeft;
[textFieldInput setDelegate:self];
[textFieldInput setKeyboardType:UIKeyboardTypeDefault];
// Set the value of the text
[textFieldInput setText:self.myTag.value];
CGSize textFieldInputSize = [textFieldInput.text sizeWithFont:textFieldInput.font];
//Set the Button Width
[textFieldInput setFrame:CGRectMake(textFieldInput.frame.origin.x, textFieldInput.frame.origin.y, textFieldInputSize.width + kTagValueInputWidthBuffer, textFieldInput.frame.size.height)];
[self.view addSubview:textFieldInput];
}
When I remove the becomeFirstResponder code, the Popover come up as normal, though no blinking insertion pointer. I tap the field, I get Insertion Pointer, X clear content button, and yes a Keyboard.
I want the keyboard to show without having to click in the new Text Field.
Thanks!
In order to become a first responder, the view must be in the view hierarchy.
You need to add your textFieldInput as a subview to something.
As per Apple's doc in UIResponder:
You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.
When you call the become first responder from the didBeginEditing you will run into an infinite loop.Reason being, when you call becomeFirstResponder it calls didBeginEditing. So It Explains the cursor blink and your statement
When I remove the becomeFirstResponder code, the Popover come up as
normal, though no blinking insertion pointer. I tap the field, I get
Insertion Pointer, X clear content button, and yes a Keyboard.
To solve your problem,
In the beginEditingMethod ,
if(texfield.tag == firstTextFieldTag)
{
//Create second TextField and make it become first responder
}
else
{
// do want you want in the beginEditing of your second textfield.
}

Resources