iphone - 嵌入 UIView 中的 UITextField 不响应触摸事件

标签 iphone cocoa-touch uitextfield becomefirstresponder

在我的项目中,我使用 UITextField 嵌入到带有圆角的纯白色 UIButton 中,以创建“漂亮”的文本字段。该代码在不同的 View 中使用了多次,因此我决定为此目的创建一个自定义 UIView。此自定义 UIView 的代码:

BSCustomTextField.h

#import <Foundation/Foundation.h>

@interface BSCustomTextField : UIView {
  UIButton* btnTextFieldContainer;
  UITextField* textField;
  NSString* text;
}

@property (retain, nonatomic) UIButton* btnTextFieldContainer;
@property (retain, nonatomic) UITextField* textField;
@property (retain, nonatomic) NSString* text;

-(id)initWithPosition:(CGPoint)position;
@end

BSCustomTextField.m

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>

@implementation BSCustomTextField
@synthesize btnTextFieldContainer, textField, text;

-(id)initWithPosition:(CGPoint)position{
   if (self == [super init]) {
      btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
      btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
      [[btnTextFieldContainer layer] setCornerRadius:3.];
      [[btnTextFieldContainer layer] setMasksToBounds:YES];
      [[btnTextFieldContainer layer] setBorderWidth:0.];

      textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

      textField.backgroundColor = [UIColor whiteColor];
      textField.clearButtonMode = UITextFieldViewModeAlways;
      textField.returnKeyType   = UIReturnKeySend;
      textField.keyboardType    = UIKeyboardTypeEmailAddress;
      textField.font               = [UIFont fontWithName:@"Helvetica-Bold" size:20.];

      [btnTextFieldContainer addSubview:textField];
      [self addSubview:btnTextFieldContainer];
   }
   return self;
}

-(void)dealloc{
   [btnTextFieldContainer release];
   [textField release];
   [super dealloc];
}
@end

因此,当在容器 View 的 viewDidLoad 中使用此 View 时(请参阅下面的代码),该 View 会正确呈现在所需的位置,并且看起来与指定的完全相同,但它不会对触摸事件使用react因此在触摸时不会成为第一响应者。

代码:

searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)];
searchTextField.textField.placeholder       = NSLocalizedString(@"lsUserName", @"");
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField];

以编程方式调用[searchTextField.textField成为FirstResponder]可以正常工作并使键盘出现。

但是,有趣的是,如果我将 BSCustomTextField 代码嵌入到我的容器中,就像这样:

UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)];
btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
[[btnTextFieldContainer layer] setCornerRadius:3.];
[[btnTextFieldContainer layer] setMasksToBounds:YES];
[[btnTextFieldContainer layer] setBorderWidth:0.];

searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

searchTextField.backgroundColor = [UIColor whiteColor];
searchTextField.clearButtonMode = UITextFieldViewModeAlways;
searchTextField.returnKeyType   = UIReturnKeySend;
searchTextField.keyboardType    = UIKeyboardTypeEmailAddress;
searchTextField.font            = [UIFont fontWithName:@"Helvetica-Bold" size:20.];
searchTextField.placeholder     = NSLocalizedString(@"lsUserName", @"");

[btnTextFieldContainer addSubview:searchTextField];
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer];
[btnTextFieldContainer release];

一切都按预期进行,文本字段对触摸使用react。 searchTextField 的类型在每种情况的头文件中正确设置。唯一的区别是,在第一种情况下,我在 UIButton 和 UITextFiled 上有一个额外的包装 UIView。我不知道该怎么做才能使文本字段成为触摸时的第一响应者。

提前非常感谢, 罗马

最佳答案

好的,我已经找到解决方案了。 raaz 对 UIResponder 类的观点让我意识到响应者链中一定有问题,因此我做了一些研究并发现了这个主题:Allowing interaction with a UIView under another UIView其中讨论了完全相同的问题。
这是 BSCustomTextField.m 的新工作代码

#import "BSCustomTextField.h"
#import <QuartzCore/QuartzCore.h>


@implementation BSCustomTextField
@synthesize btnTextFieldContainer, textField, text;

-(id)initWithPosition:(CGPoint)position{
  if (self == [super init]) {
    btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)];
    btnTextFieldContainer.backgroundColor = [UIColor whiteColor];
    [[btnTextFieldContainer layer] setCornerRadius:3.];
    [[btnTextFieldContainer layer] setMasksToBounds:YES];
    [[btnTextFieldContainer layer] setBorderWidth:0.];

    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)];

    textField.backgroundColor = [UIColor whiteColor];
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.returnKeyType   = UIReturnKeySend;
    textField.keyboardType    = UIKeyboardTypeEmailAddress;
    textField.font             = [UIFont fontWithName:@"Helvetica-Bold" size:20.];

    [btnTextFieldContainer addSubview:textField];
    [self addSubview:btnTextFieldContainer];
  }
  return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    return YES;
}

-(void)dealloc{
  [btnTextFieldContainer release];
  [textField release];
  [super dealloc];
}
@end 

由于可点击区域填充了整个 BSCustomTextField View ,我们可以简单地在 pointInside:withEvent 中返回 YES:

感谢大家为我指明了正确的方向。

关于iphone - 嵌入 UIView 中的 UITextField 不响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4553254/

相关文章:

iphone - Objective-C:如何检测字符串中的 http URL?

ios - 如何为通知中心全宽制作 iOS 应用程序扩展?

ios - UIScrollView 不使用 UItextfields 滚动

iphone - IOS编译源代码出错?

iphone - Coredata iPhone 到 iPad/Mac 是否支持 iCloud?

ios - 在 UiNavigationBar 上禁用多点触摸

ios - 我可以在 UITextField 中有多个 leftOverlay 项目或同时有 2 个独立的文本吗?

iphone - 如何在iPhone sdk中以编程方式获取设备名称?

ios - 拦截 UITextField touch

iphone - 调用 textfieldshouldreturn 会导致调用异常