UITextView won't resignFirstResponder so another can becomeFirstResponder - objective-c

I have a toolbar in my UITextfields' inputAccessoryView. If the 'next' button is hit it makes the next textfield in an orderedSet of all my textfields become the first responder. THAT works perfectly.
BUT I can't get the 'previous' textfield to becomeFirstResponder.
I've checked in the console and the textfield does call textFieldShouldBeginEditing and I am returning YES but it never calls textFieldDidBeginEditing and the textfield that should resignFirstResponder never calls textFieldShouldEndEditing.
So the textfield is getting the message to becomeFirstResponder but doesn't.
- (IBAction)keyboardBarButtonDidTouch:(UIBarButtonItem *)sender
{
if (sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue-1)] becomeFirstResponder];
}
if (sender==self.nextBarButton&&self.activeTextFieldArrayIndex.intValue<(self.textfields.count-1))
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue+1)] becomeFirstResponder];
}
if (sender==self.doneBarButton)
{
[self.activeTextField resignFirstResponder];
}
}
The weirdest thing is that if I force the textfield to resignFirstResponder by some action that just does that, the 'previous' textfield suddenly becomesFirstResponder. Like it was being added to a list of responders and it became its turn....

And as is often the case when strange things happen, it was unrelated to becoming first responder. I was accidentally changing my activeTextfield pointer to nil in the middle of a bunch of logic by way of the textfield delegate. ^o^
Disregard!

1) Try making your code less error prone.
instead of this
if(sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
do this
if ((sender==self.previousBarButton) && (self.activeTextFieldArrayIndex.intValue>0))
2) use textfield.tag property and set unique tags for these text fields. and do your stuff about become/resign first reponders by using
(uitextfield *)field[self viewWithTag: uniqueTag];

Related

How can I make my NSTextField NOT highlight its text when the application starts?

When my application launches, the first NSTextField is being selected like this:
I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.
I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.
How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.
I'm running Yosemite 10.10.2.
Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.
To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:
- (BOOL) becomeFirstResponder
{
BOOL responderStatus = [super becomeFirstResponder];
NSRange selectionRange = [[self currentEditor] selectedRange];
[[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];
return responderStatus;
}
The resulting behavior is that the field does not select the text when it gets the focus.
To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[_menuController doInitWithContentView:_window.contentView];
}
The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.
I am using IB, there's a property on NSTextField called Refuses First Responder. Ticking that will prevent the highlighting of the text field immediately after the window is presented. There's some more detailed info about Refuses First Responder in this question.
No need to subclass. Simply set refusesFirstResponder = YES;
NSTextField *textField = [NSTextField new];
textField.refusesFirstResponder = YES;
That's it! Do that and it won't highlight the text in the field.

iOS7:editable textfield cells in tableview is not working properly

Editable textfield cells Tableview is causing problem on keyboard tab button everytime it is calling textfieldshouldbeginediting even if i am in first textfield it is not going to nextfield.
It is going to last textfield and if popover is availabe it will crash.How can i fix this so that if enter tab then it has to resign current responder in textfielddidendediting and it should not go to textfieldshouldbegin editing.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
PickerViewController *selectOperatorController;
NSLog(#"tag %d",textField.tag);
return NO;
}
I also declared textfield delegates like didendediting and shouldendediting
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
activeField = nil;
if (self.chooseOperatorController) {
[self.chooseOperatorController dismissPopoverAnimated:YES];
}
return YES;
}
This is not as issue in iOS 6.But it is in iOS 7.
textfieldshouldbegin editing will not allow desktop keyboard tab button input,it cannot judge to move between the textfields.If we add textfielddidbeginediting we can move the controls,eventhough we have two methods we can move by using keyboard tab.So textfielddidbeginediting is mandatory if we want to move bewtween available textfields.

Can't type in UITextField if presentViewController while editing

I have a ConfigureViewController that contains some UIButtons and a UITextField * MyTextField. Each of the buttons, when pressed, brings up a dialog-style viewcontroller using presentViewController:animated:completion:. However, if I tap one of those buttons while editing the text field, when i close the dialog and return to the original screen, i am unable to return focus to or type in the text field.
This is the method that is invoked when the button is tapped.
-(void)AdvancedInfoButtonPressed :(id)sender
{
AdvancedInfoPopViewController *myAdvancedInfoViewController = [[AdvancedInfoPopViewController alloc] init];
[myAdvancedInfoViewController setDelegateAndDevice :self :Current_Device];
myAdvancedInfoViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController :myAdvancedInfoViewController animated :NO completion :nil];
}
Without explicitly removing focus from MyTextField, presenting the AdvancedInfoViewController does dismiss the keyboard automatically.
I suspect the problem is that MyTextField still thinks it has focus (even though the keyboard and blinking cursor have disappeared) and so does not allow itself to become the first responder again. Along these lines, I have found that if i add [MyTextField resignFirstResponder] before presenting the dialog viewcontroller, the problem goes away.
However, this does not seem like a very good solution because it means having to remember to resign this textfield (or any other text fields) as the first responder in several places (leading to code that is difficult to maintain). My question is: are there any events i can hook into either when ConfigureViewController is about to be partially obscured by AdvancedInfoViewController (or when AdvancedInfoViewController is dismissed and focus is returned to the ConfigureViewController) in which i can add some logic to clean up MyTextField's firstResponder status?
I've tried viewWillDisappear and viewWillAppear but they are never called on the ConfigureViewController.
I've also tried adding textFieldDidEndEnding to the text field's delegate but, despite it being called, it did not fix the problem.
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[MyTextField resignFirstResponder];
}
You can use [self.view endEditing:YES] to resign all first responders in the case you are presenting myAdvancedInfoViewController.
To return focus to the textField after this event occurs you will need to keep track of which textField was active at the time myAdvancedInfoViewController was presented. When the myAdvancedInfoViewController is dismissed call UITextField's becomeFirstResponder method for the appropriate text field.

Enabling Done Button When User Changes Values - Xcode

I am trying to make it to where if any value is typed on the keyboard without hitting the return key and two of the labels have been changed from "Select" to something else, the button up at the top becomes enabled. However, I have tried using an IBAction saying:
- (IBAction)valuesChanged {
if (textField.text != nil && ![labelOne.text isEqualToString:#"Select"] && ![labelTwo.text isEqualToString:#"Select"]) {
NSLog(#"Success");
}
else {
NSLog(#"No Success");
}
}
But I have realized that this does not work because:
The textfield does not work when I put the IBAction Sent Event as "Value Changed"
The labels won't accept an action.
How do I go about doing this?
The text field delegate method that tells you that the user is typing a character in the text field or otherwise changing its contents is textField:shouldChangeCharactersInRange:replacementString:. Implement it in your text field delegate and respond as appropriate. You will also just return YES.
One reason your original code couldn't be hooked up might be that you have not used the canonical form of an IBAction method; it should be
- (IBAction)valuesChanged:(id)sender {
Another problem in your original code is that a UITextField does not emit Value Changed. What you wanted was Editing Changed. But the delegate method works just as well.
Try UITextField delegate methods.
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;

UITableView reloadData causes UITextField to resignFirstResponder

I have a textField that is set to change the tableView's dataSource with each letter that's entered (and call reloadData).
But for some reason, every time a letter is entered, the keyboard is dismissed.
Anyone know why?
Your text field is resigning because reloaded cells are sent a -resignFirstResponder message due to the fact that their survival is not guaranteed after a reload. See this related question for more.
Use this method textFieldShouldReturn: and add UITextFieldDelegate delegate in yourClass.h file. set delegate to yourTextfield and write following code in viewDidLoad method.
yourTextfield.delegate = self;
and also implement the textFieldShouldReturn: as following as
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
I think it will be helpful to you.