UITextField拒绝允许用户输入

时间:2012-12-10 13:27:39

标签: objective-c ios uibutton uitextfield

我在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“。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

进一步编辑:这是我在调试提示符下输入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) 

4 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

试试这个

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

答案 3 :(得分:0)

从代码

创建文本字段时,必须定义文本字段的框架
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
tf.backgroundColor = [UIColor greenColor];

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

-(void)viewDidLoad{

    [self.nameField setUserInteractionEnabled:YES];
}