简体   繁体   English

点击UITextField时,键盘和光标都不会出现

[英]Neither keyboard nor cursor are appearing when a UITextField is tapped

I am testing out a simple application on Xcode 5.0.2. 我正在Xcode 5.0.2上测试一个简单的应用程序。 Using storyboard, I've dragged two view controllers that are connected by segues. 使用storyboard,我拖动了两个由segue连接的视图控制器。 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 textFieldViewController.h文件

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

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

- (IBAction)textFieldDoneEditing:(id)sender;


- (IBAction)backgroundTap:(id)sender;

textFieldViewController.m file textFieldViewController.m文件

 - (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 / iOS 8也会发生这种情况:

Xcode 6: Keyboard does not show up in simulator Xcode 6:键盘不会出现在模拟器中

确保您的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 你可以试试这个并使用storyboard设置textFields委托

or use 或使用

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

an in textFieldViewController.h file 在textFieldViewController.h文件中

change to @interface textFieldViewController : UIViewController <UITextFieldDelegate> 更改为@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. 一切都很好,直到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). 所以我创建了一个UITextField,当(第一种情况)我的视图即将被添加为子视图时(第二种情况)视图被初始化(initWithFrame:方法)。 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的初始化代码放入lazy-getter并调度此方法的延迟调用。

-(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. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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