UITextField 拒绝允许用户输入

Posted

技术标签:

【中文标题】UITextField 拒绝允许用户输入【英文标题】:UITextField refuses to allow user input 【发布时间】:2012-12-10 13:27:38 【问题描述】:

我在 Interface Builder 中设置了一个 UITextField 并将其与属性 self.nameField 连接起来。我的问题是,点击它什么也没做——用户不能输入任何文本;甚至没有光标。如果我添加becomeFirstResponder,那么键盘会自动显示。我想要的是当且仅当用户点击该字段输入文本时,键盘才会显示。

涉及的方法如下:

-(void)displayOptOutScreen

    if (!self.nameField)
    
        self.nameField=[[UITextField alloc] init];
    
    self.nameField.hidden=NO;
    self.nameField.delegate = self;
    self.nameField.returnKeyType = UIReturnKeyDone;
    [self textFieldShouldReturn:self.nameField];
    [self.textView resignFirstResponder]; //This is a UITextView, created programmatically
    //[self.nameField becomeFirstResponder];  //Keyboard automatically displays.

对于它的价值,有一个UIButton 也不接受触摸。 UITextView 可以毫无问题地接受文本。

这是一个选项卡式应用程序。在阅读了其他 Stack Overflow 问题后,我尝试将以下几行的各种组合添加到应用程序委托中:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[GHHaikuViewController alloc] initWithNibName:@"GHHaikuViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

但是根据我包含或不包含的行,我会遇到各种错误,并且最终没有任何效果。 (UITextField 所在的视图控制器不是GHHaikuViewController 而是GHComposeViewController,但是当我尝试使用它时出现错误。)

关于我做错了什么有什么想法吗?

编辑:这里是 Interface Builder 设置的屏幕截图。抱歉,他们分手了——我正在开发 MacBook Air 11"。

进一步编辑:这是我在调试提示符下输入 po [[[[UIApplication sharedApplication] delegate] window] recursiveDescription] 时的输出。

(id) $1 = 0x0ad4bb80 <UIWindow: 0xad48ac0; frame = (0 0; 320 480); autoresize = W+H; layer = <UIWindowLayer: 0xad44010>>
   | <UILayoutContainerView: 0x9e74450; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x9e74510>>
   |    | <UITransitionView: 0xaa46f80; frame = (0 0; 320 431); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xaa47050>>
   |    |    | <UIViewControllerWrapperView: 0x9e90760; frame = (0 20; 320 411); autoresize = W+H; layer = <CALayer: 0x9e907c0>>
   |    |    |    | <UIView: 0x9e8c870; frame = (0 0; 320 411); autoresize = W+H; gestureRecognizers = <NSArray: 0x9e8bbd0>; layer = <CALayer: 0x9e8c8d0>>
   |    |    |    |    | <UIImageView: 0x9e8c790; frame = (0 0; 320 411); clipsToBounds = YES; opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x9e8c7f0>>
   |    |    |    |    | <UITextField: 0x9e82610; frame = (40 189; 240 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x9e6ea90>; layer = <CALayer: 0x9e83720>>
   |    |    |    |    |    | <UITextFieldRoundedRectBackgroundView: 0x9e73ac0; frame = (0 0; 240 30); userInteractionEnabled = NO; layer = <CALayer: 0x9e73b80>>
   |    |    |    |    |    |    | <UIImageView: 0x9e73cd0; frame = (0 0; 8 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e73d30>>
   |    |    |    |    |    |    | <UIImageView: 0x9e73d80; frame = (8 0; 224 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e73de0>>
   |    |    |    |    |    |    | <UIImageView: 0x9e73e10; frame = (232 0; 8 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e73e70>>
   |    |    |    |    |    | <UITextFieldLabel: 0x9e8afa0; frame = (7 7; 226 15); text = 'Name (optional)'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e8af20>>
   |    |    |    |    | <UIButton: 0xad6fb00; frame = (236 221; 44 44); clipsToBounds = YES; opaque = NO; autoresize = TM+BM; layer = <CALayer: 0xad6fbf0>>
   |    |    |    |    |    | <UIImageView: 0x9e921c0; frame = (14 14; 16 16); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e922d0>>
   |    |    |    |    |    | <UIButtonLabel: 0x9e8a6f0; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9e8a790>>
   |    |    |    |    | <UITextView: 0xa3de800; frame = (20 90; 300 480); text = '
I hope to update the May...'; clipsToBounds = YES; gestureRecognizers = <NSArray: 0x9e72ef0>; animations =  transition=<CATransition: 0x9e8eea0>; ; layer = <CALayer: 0x9e5d770>; contentOffset: 0, 0>
   |    |    |    |    |    | <UIWebDocumentView: 0xa3dec00; frame = (0 0; 300 188); text = '
I hope to update the May...'; opaque = NO; userInteractionEnabled = NO; gestureRecognizers = <NSArray: 0x9e8d5a0>; layer = <UIWebLayer: 0x9e8d220>>
   |    |    |    |    |    |    | <TileHostLayer: 0x9e8d2b0> (layer)
   |    |    |    |    |    |    |    | <TileLayer: 0x9e90fb0> (layer)
   |    | <UITabBar: 0xad41b80; frame = (0 431; 320 49); autoresize = W+TM; layer = <CALayer: 0xad41c70>>
   |    |    | <_UITabBarBackgroundView: 0xad47950; frame = (0 0; 320 49); opaque = NO; autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0xad47a00>>
   |    |    | <UITabBarButton: 0xad42ff0; frame = (2 1; 76 48); opaque = NO; layer = <CALayer: 0xad453e0>>
   |    |    |    | <UITabBarSwappableImageView: 0xad45670; frame = (14 2; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad45740>>
   |    |    |    | <UITabBarButtonLabel: 0xad42410; frame = (24 34; 28 13); text = 'Home'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad42500>>
   |    |    | <UITabBarButton: 0xad46b10; frame = (82 1; 76 48); opaque = NO; layer = <CALayer: 0xad46460>>
   |    |    |    | <UITabBarSelectionIndicatorView: 0xad5cf90; frame = (0 2; 76 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad5d2f0>>
   |    |    |    | <UITabBarSwappableImageView: 0xad46490; frame = (14 2; 48 32); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad46510>>
   |    |    |    | <UITabBarButtonLabel: 0xad46790; frame = (15 34; 46 13); text = 'Compose'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad40650>>
   |    |    | <UITabBarButton: 0xad46630; frame = (162 1; 76 48); opaque = NO; layer = <CALayer: 0xad47080>>
   |    |    |    | <UITabBarButtonLabel: 0xad46df0; frame = (29 34; 19 13); text = 'Buy'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad466f0>>
   |    |    | <UITabBarButton: 0xad47270; frame = (242 1; 76 48); opaque = NO; layer = <CALayer: 0xad47770>>
   |    |    |    | <UITabBarButtonLabel: 0xad47330; frame = (15 34; 47 13); text = 'Feedback'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xad473d0>>
   |    |    | <UIImageView: 0xad48130; frame = (0 -3; 320 3); opaque = NO; autoresize = W+BM; userInteractionEnabled = NO; layer = <CALayer: 0xad48190>>
(lldb) 

【问题讨论】:

在左侧的 Interface Builder 中为该视图控制器的层次结构截图,请展开所有内容。 完成了——如果我只是搞砸了一些设置,那就太好了! 遗憾的是,事实并非如此。这个 displayOptOut 方法什么时候被调用? @JoelDerfner 检查我的答案,我已经编辑了它,我希望它能解决问题 确保为您的 UITextField 和 UIButton 设置了 userInteractionEnabled 和 enabled 属性。 【参考方案1】:

假设您正确处理了 UIControls(例如,将所有 userInteractionEnabled 属性设置为 YES),这听起来好像您在 UIControls 之上设置了一个不可见的 UIView。

一旦您的应用程序在模拟器中运行,您可以通过暂停应用程序(使用代码下的小暂停按钮)并在调试提示符中键入以下命令来手动检查:

po [[[[UIApplication sharedApplication] delegate] window] recursiveDescription]

这将打印出 UIWindow 及其所有子视图的描述。您可以通过某种方式检查您的 UIControls 之上是否有 UIView。

【讨论】:

调试提示在哪里? 我已编辑问题以包含您建议的命令的输出。我不明白,但我认为它包含我寻求的答案。你能帮我解释一下并告诉我该怎么做吗? 我最终添加了[self.view bringSubviewToFront:self.nameField]; 并解决了问题!【参考方案2】:

是否禁用了用户交互的视图的按钮和文本字段子视图?

【讨论】:

他们似乎不是,唉。那会很棒——很容易解决。 :(【参考方案3】:

试试这个

for(UIView *view in [self view].subViews)
    [view setUserInteractionEnabled:YES];

【讨论】:

不幸的是,这并没有解决问题。 :( 不过谢谢。【参考方案4】:

当你从代码创建文本字段时,你必须定义它的框架

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
tf.backgroundColor = [UIColor greenColor];

如果您从 xib 文件创建它,请确保选中 用户交互启用 检查或在视图委托中进行检查

-(void)viewDidLoad

    [self.nameField setUserInteractionEnabled:YES];

【讨论】:

以上是关于UITextField 拒绝允许用户输入的主要内容,如果未能解决你的问题,请参考以下文章

不允许用户输入数字1作为UITextField中的第一个数字

仅允许 UITextField 输入 Swift iOS8 的电子邮件 [重复]

如何在 Swift 中防止 UITextField 中出现空白或空格?

如何在 swift 2.0 中只允许 UITextfield 中的某些数字集

如何限制 UIAlertView UITextField 中的字符输入

如何使 UIButton 导致 UITextField 开始编辑文本