UITextField Will Not Be FirstResponder

I had a problem getting a text field to accept the getFirstResponder directive.

I provide a custom mechanism for creating a title in the navigation bar. I have another view manager that successfully uses the same technique. On viewDidAppear, I run:

- (void)addTitleTextField { CGRect textFrame = self.parentViewController.navigationController.navigationBar.frame; textFrame.size.width = 300.0; textFrame.origin.y = (768.0 - 300.0)/2; textFrame.size.height = 30.0; textFrame.origin.x = 7.0; self.titleTextField = [[UITextField alloc] initWithFrame:textFrame]; self.titleTextField.placeholder = NSLocalizedString(@"New Multiple Choice Quiz", @"New Multiple Choice Quiz"); self.titleTextField.borderStyle = UITextBorderStyleRoundedRect; self.titleTextField.font = [UIFont boldSystemFontOfSize:20.0]; self.titleTextField.textAlignment = NSTextAlignmentCenter; self.titleTextField.backgroundColor = [UIColor whiteColor]; self.titleTextField.textColor = [UIColor blackColor]; self.titleTextField.delegate = self; [self.titleTextField setAutocorrectionType:UITextAutocorrectionTypeNo]; self.titleTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [self.titleTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; self.activeTextField = self.titleTextField; self.parentViewController.navigationItem.titleView = self.titleTextField; [self.titleTextField becomeFirstResponder]; } 

self.titleTextField will allow me to set a text value, but if you check the use of canBecomeFirstResponder, it will return NO. As you can see, I set this to parentViewController. I tried using a delegate to try setting the parentViewController. When I do a delegate and check if textField canBecomeFirstResponder returns YES, but I still can not get it to accept the firstResponder order. Any ideas? The docs say, "The responder object becomes only the first responder if the current responder can leave the status of the first responder (canResignFirstResponder) and the new responder can become the first responder."

+4
source share
2 answers

Are you telling UITextField to become a selector in the background thread?

 [textField performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil waitUntilDone:YES]; 

Justification . Calling UIKit methods (i.e. updating the view) using a method other than the main thread will not work. This may happen. (It is unclear where the addTitleTextField method is called from.)

Is there another defendant who needs some time to resign?

 [textField performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0]; 

Justification . If another field hangs on the first responder (or during the process of its resignation), it will give time to clear the resignation, waiting for the next cycle of the cycle., Usually the next cycle of the cycle will be sufficient to clear the previous responder, or you can try a short delay, for example, 0.05.

+6
source

You may need to add textField to view the hierarchy:

 v.addSubview(textField) 
0
source

Source: https://habr.com/ru/post/1485213/


All Articles