Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField becomeFirstResponder dos not show keyboard

In my program I need to show text from textfield, which is not user interactive in toolbar, which is accessory view for keyboard. This part works when I press button. Then if device rotates I need to hide keyboard. This part also works fine. But then when I come back to portret and press button which needs to show keyboard nothing heppens.

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

like image 806
v_semenov Avatar asked Jan 09 '23 18:01

v_semenov


1 Answers

By default the iOS Simulator has the hardware keyboard connected. This behavior is similar to how a device behaves when a bluetooth keyboard is connected.

You can tap cmd-k to toggle the software keyboard on/off just like you can use the eject button on a bluetooth keyboard.

If you want to test the disconnected configuration, you can disconnect the hardware keyboard with shift-cmd-k

like image 79
Jeremy Huddleston Sequoia Avatar answered Jan 19 '23 04:01

Jeremy Huddleston Sequoia