简体   繁体   中英

Neither keyboard nor cursor are appearing when a UITextField is tapped

I am testing out a simple application on Xcode 5.0.2. Using storyboard, I've dragged two view controllers that are connected by segues. On first view controller there is a button, after pressing that button next controller shows up which has two textfields. When I tap the textfield, the keyboard is not showing up and the text cursor is missing.

textFieldViewController.h file

@property (weak, nonatomic) IBOutlet UITextField *nameField;

@property (weak, nonatomic) IBOutlet UITextField *numberField;

- (IBAction)textFieldDoneEditing:(id)sender;


- (IBAction)backgroundTap:(id)sender;

textFieldViewController.m file

 - (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)backgroundTap:(id)sender {
    [self.nameField resignFirstResponder];
    [self.numberField resignFirstResponder];
}

screenshot 在此输入图像描述

This can also happen with Xcode 6/iOS 8 because of simulator settings:

Xcode 6: Keyboard does not show up in simulator

确保您的UserInteraction of UITextfieldEnabled并且您的UITextfield上没有任何视图。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{//Text Field Delegate

     [textField resignFirstResponder];

    return TRUE;
}

can u try this and set the textFields delegate using storyboard

or use

- (void)viewDidLoad
{
  nameField.delegate = self;
  numberField.delegate = self;
}

an in textFieldViewController.h file

change to @interface textFieldViewController : UIViewController <UITextFieldDelegate>

I have had this issue and in my case, I have a custom view that displays various subviews on demand. Everything was fine, until the release of iOS 7.1.

So I was creating an UITextField when (1st case) my view was about to be added as subview and when (2nd case) the view was initialized (initWithFrame: method). I have seen so many solutions where the responder chain was modified or the tint color changed. But in my case, I got my stuff behaving normally only with that one thing:

I placed the initialization code for the UITextField into a lazy-getter and scheduled a delayed call of this method.

-(UITextField*)textField {
    if (!_tf) {
        _tf = [[UITextField alloc] initWithFrame:self.bounds]
        // (... more code to set up the text field ...)
        [self addSubview:_tf];
    }
    return _tf;
}

And then, (I had already overridden this method, so I deceided to call this here):

-(void)didMoveToSuperview {
    [super didMoveToSuperview];
    if (self.superview) {
        // (... other code ...)
        [self performSelector:@selector(textField) withObject:nil afterDelay:0.04];
    }
}

And it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM