1

我已经在 Interface Builder 中设置了一个UITextField并将其与 property 连接起来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不是GHHaikuViewControllerbut GHComposeViewController,但是当我尝试使用它时出现错误。)

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

编辑: 这是界面生成器设置的屏幕截图。抱歉,他们分手了——我正在开发 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) 
4

4 回答 4

2

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

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

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

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

于 2012-12-10T13:55:01.647 回答
1

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

于 2012-12-10T13:32:01.843 回答
1

试试这个

for(UIView *view in [self view].subViews){
    [view setUserInteractionEnabled:YES];
}
于 2012-12-10T13:36:08.977 回答
0

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

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

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

-(void)viewDidLoad{

    [self.nameField setUserInteractionEnabled:YES];
}
于 2012-12-10T14:01:37.500 回答