TextField becomes FirstResponder dos does not show keyboard

In my program, I need to show text from a text field that is not an interactive user in the toolbar, which is an accessory for the keyboard. This part works when I click the button. Then, if the device rotates, I need to hide the keyboard. This part also works great. But then, when I return to the portrait and press the button that should show the keyboard, nothing will happen.

Here is my code:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController{
    UITextField *invisibleTextfield;
    UITextField *fieldToEnterText;
}
- (IBAction)buttonAction:(id)sender {
    [invisibleTextfield becomeFirstResponder];
}

#pragma mark - Text Field
- (UIToolbar *)keyboardToolBar {

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    [toolbar sizeToFit];
    [toolbar setOpaque:NO];
    [toolbar setTranslucent:YES];
    UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:fieldToEnterText];
    UIBarButtonItem *fixItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];


    NSArray *itemsArray = @[fixItem,textFieldItem,fixItem];

    [toolbar setItems:itemsArray];

    return toolbar;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([invisibleTextfield isFirstResponder]) {
        NSLog(@"did began editing invisible");
        [fieldToEnterText becomeFirstResponder];
        fieldToEnterText.text = _textField.text;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    _textField.text = fieldToEnterText.text;
}

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if ([fieldToEnterText isFirstResponder]) {
        [fieldToEnterText resignFirstResponder];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    fieldToEnterText = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-30, 30)];
    fieldToEnterText.borderStyle = UITextBorderStyleRoundedRect;
    fieldToEnterText.delegate = self;
    [fieldToEnterText setKeyboardType:UIKeyboardTypeNumberPad];
    [fieldToEnterText setTextAlignment:NSTextAlignmentCenter];

    invisibleTextfield = [[UITextField alloc] init];
    [invisibleTextfield setKeyboardType:UIKeyboardTypeNumberPad];
    invisibleTextfield.inputAccessoryView = [self keyboardToolBar];
    invisibleTextfield.delegate = self;
    [self.view addSubview:invisibleTextfield];
    _textField.text = @"Text";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Interface

+4
source share
3 answers

By default, a hardware keyboard is connected in iOS Simulator. This behavior is similar to device behavior when a Bluetooth keyboard is connected.

cmd-k, , Bluetooth.

, shift-cmd-k

+9

, , [invisibleTextfield becomeFirstResponder];, [fieldToEnterText resignFirstResponder];. ? ?

UPDATE:

? . KB .

:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardMNotification:)
                                         name:UIKeyboardWillShowNotification
                                       object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(keyboardMNotification:)
                                         name:UIKeyboardWillHideNotification
                                       object:nil];

, : UIKeyboardWillShowNotification

0

Like in iOS8, calling beFirstResponder in a UITextView does not display the keyboard for me on the iOS simulator, but it works on a real device. Therefore, be sure to check it on the device itself.

0
source

All Articles