Iphone 如何让键盘消失?
我有一个显示在UITableViewCell中的文本字段,我希望当用户触摸屏幕上除文本字段以外的任何地方时,能够隐藏键盘。我知道[现场辞职第一响应者];,但我不知道如何截取UITableView背景上的触动,以便调用“resignFirstResponder”Iphone 如何让键盘消失?,iphone,ios4,Iphone,Ios4,我有一个显示在UITableViewCell中的文本字段,我希望当用户触摸屏幕上除文本字段以外的任何地方时,能够隐藏键盘。我知道[现场辞职第一响应者];,但我不知道如何截取UITableView背景上的触动,以便调用“resignFirstResponder” 任何帮助都将不胜感激。尝试实现UITextFieldDelegate方法的-(void)textfieldDendediting:(UITextField*)textField方法非常简单:创建一个透明的UIView对象,在您想要的区域接
任何帮助都将不胜感激。尝试实现
UITextFieldDelegate
方法的-(void)textfieldDendediting:(UITextField*)textField
方法非常简单:创建一个透明的UIView对象,在您想要的区域接收触摸,当触摸在该视图的边界内时,呼叫辞职第一响应者
// somewhere in a view controller
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:backgroundView];
// in the touchesBegan:withEvent: method, for example
UITouch *touch = [[event allTouches] anyObject];
if ([field isFirstResponder] && [touch view] == backgroundView) {
[field resignFirstResponder];
}
或者,您可以跳过backgroundView内容,只需在touchBegind:withEvent:method中添加如下条件语句:
UITouch *touch = [[event allTouches] anyObject];
if ([field isFirstResponder] && [touch view] != field) {
[field resignFirstResponder];
}
如果触摸是正确的!(不是)在
字段
的范围内,则要删除键盘。唯一的方法是对UITableView进行子类化,在子类化的UITableView中实现touchsbegind方法,并将UITextField对象发送到UITableView。下面是它的样子-
// GRTableView.h
// StackOverflow Example
//
// Created by Raphael Caixeta on 8/13/10.
// Copyright 2010 Raphael Caixeta. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface GRTableView : UITableView <UITableViewDelegate> {
UITextField *textField;
}
@property(nonatomic, retain) UITextField *textField;
@end
//
// GRTableView.m
// StackOverflow Example
//
// Created by Raphael Caixeta on 8/13/10.
// Copyright 2010 Raphael Caixeta. All rights reserved.
//
#import "GRTableView.h"
@implementation GRTableView
@synthesize textField;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
- (void)dealloc {
[super dealloc];
}
@end
//GRTableView.h
//堆栈溢出示例
//
//拉斐尔·凯克塞塔于2010年8月13日创作。
//版权所有2010拉斐尔·凯克塞塔。版权所有。
//
#进口
@界面GRTableView:UITableView{
UITextField*textField;
}
@属性(非原子,保留)UITextField*textField;
@结束
//
//GRTableView.m
//堆栈溢出示例
//
//拉斐尔·凯克塞塔于2010年8月13日创作。
//版权所有2010拉斐尔·凯克塞塔。版权所有。
//
#导入“GRTableView.h”
@表视图的实现
@合成文本场;
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[textField resignFirstResponder];
[超级触摸开始:触摸事件:事件];
}
-(无效)解除锁定{
[super dealoc];
}
@结束
然后在常规文件中分配UITableView,只需分配子类视图并将文本字段传递给子类即可。希望对您有所帮助。您可以在以下情况下隐藏键盘
- (void)tableView:(UITableView*)tableView DidSelectRowatineXpath:(NSIndexPath*)indexPath{}
- (void)TextField:(UITextField*)TxtFld textDidChange:(NSString*)searchText{}
但它仍然不起作用,然后指定发生的确切问题 有一种比标准的“UITableView子类”或“添加UIView和handle的子类”更简单的方法来实现这一点。我在我想要处理触摸的区域上创建一个背景颜色清晰的UIButton。将其添加到视图时,我将其隐藏属性设置为“是” 当文本字段开始编辑时,我将按钮的hidden属性设置为NO
然后,该按钮简单地响应UITouchUpInside控件事件,方法调用[textField resignFirstResponder]和button.hidden=YES 由于某些原因,没有调用我的touchesbeent方法。有什么建议吗?My View和UITableView将userInteractionEnabled和MultipleToTouchEnabled设置为YES。请确保ToucheSBegind:withEvent:方法具有正确的签名,并且在UIViewController中实现,而不是在视图本身中实现。这不是“实现此操作的唯一方法”。也可以查看列表中的其他有效答案。自己尝试一下。您将看到单击UITableView的“背景”将被阻止。你真的认为如果其他的例子都有效的话,我会费心去创建一个例子吗?