Ios UITextField不响应触摸,但响应清除按钮

Ios UITextField不响应触摸,但响应清除按钮,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我有UITextField,我需要使它不响应触摸,因为它位于UITableViewCell上,当用户触摸textField时,didselectrowatinexpath不会被调用 这个文本字段也有clear按钮,我只需要检测用户在clear按钮上的点击,而不是在rest文本字段区域 我试过,textField.userInteractionEnabled=NO。在这种情况下,“清除”按钮当然不起作用,textField.enabled=NO UITextField *textField =

我有
UITextField
,我需要使它不响应触摸,因为它位于
UITableViewCell
上,当用户触摸
textField
时,
didselectrowatinexpath
不会被调用

这个文本字段也有clear按钮,我只需要检测用户在clear按钮上的点击,而不是在rest文本字段区域

我试过,
textField.userInteractionEnabled=NO
。在这种情况下,“清除”按钮当然不起作用,
textField.enabled=NO

 UITextField *textField = [[UITextField alloc] initWithFrame:someFrame];
 textField.textAlignment = NSTextAlignmentLeft;
 textField.clearButtonMode = UITextFieldViewModeAlways;
 textField.delegate = self;
请帮忙


提前感谢。

在这种情况下,可以使用一种变通方法。您必须创建一个视图来屏蔽textfield的文本区域,但不能像这样清除按钮

我们认为

someFrame = (20,50,100,44)
以及
20*20周围的文本字段按钮

所以我们的观点是

UIView *maskView = [[UIView alloc] initWithFrame: CGRectMake (20,50,80,44)]; 
maskView.backgroundColor = [UIColor clearColor];
在添加文本字段后添加此视图。它会屏蔽textfield用于文本的区域,并保持按钮区域不变


请记住,这不是测试,只是一种变通方法。所以除了我推荐你使用的方法之外,还有其他方法吗。并根据您的需要更改值。

您是从nib加载单元格还是从其他位置加载单元格。我刚刚试着制作我自己的定制电池,如下图所示

为uitextfield分配标记(在我的示例中是9)

视图索引中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell ;

        cell = [tableView dequeueReusableCellWithIdentifier:@"purchaseNibCell"];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"PurchaseNibCell" owner:self options:nil];
            cell                    =   self.purchaseCell;
            self.purchaseCell       =   nil;

        }
        UITextField *text           =   (UITextField*)[cell viewWithTag:9];
        text.enabled                =   NO; <------ if you have not done in the xib file.
        cell.backgroundView         =   [[CustomCellBackground alloc] init];
        UIImageView *accessoryView  =   [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
        cell.accessoryView          =   accessoryView;

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格;
cell=[tableView dequeueReusableCellWithIdentifier:@“purchaseNibCell”];
如果(单元格==nil){
[[NSBundle mainBundle]loadNibNamed:@“PurchaseNibCell”所有者:自选项:nil];
cell=self.purchaseCell;
self.purchaseCell=nil;
}
UITextField*text=(UITextField*)[带标记的单元格视图:9];

text.enabled=NO;
-(BOOL)textField应该开始编辑:(UITextField*)textField{return NO;}
将使textfield不可编辑。用户可以编辑textfield中的文本吗?如果不能,只需使用
UILabel
,设置用户交互,并向表格单元格添加附件按钮以触发清除操作。当用户点击附件按钮时,清除标签。我实现了-(BOOL)textfield应开始编辑:(UITextField*)textField{return NO;},但是textField仍然响应触摸,我需要它不响应touches@Ric佩罗特,我喜欢你的解决方案。谢谢:)