Iphone 在UITextView上失去焦点时隐藏键盘
所以我有一个UITextView,我用它来允许用户提交一些文本 我的问题是,我似乎不知道如何允许用户通过点击离开UITextView来“取消”。在我的视图中:Iphone 在UITextView上失去焦点时隐藏键盘,iphone,objective-c,cocoa-touch,uikit,uitextview,Iphone,Objective C,Cocoa Touch,Uikit,Uitextview,所以我有一个UITextView,我用它来允许用户提交一些文本 我的问题是,我似乎不知道如何允许用户通过点击离开UITextView来“取消”。在我的视图中: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; // pass touches up to viewController [self.nextRespo
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// pass touches up to viewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
在我的viewcontroller中:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (keyboardShown && [touch view] != self.TextView)
{
[self.TextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
keyboardShown = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([self.yourTextView isFirstResponder]) {
[self.yourTextView resignFirstResponder];
}
}
简化:
以下插座在您的xib中正确连接
IBOutlet UITextView *myTextView;
在视图控制器中使用此选项:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (keyboardShown && [touch view] != self.TextView)
{
[self.TextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
keyboardShown = YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([self.yourTextView isFirstResponder]) {
[self.yourTextView resignFirstResponder];
}
}
点击文本视图之外的视图控制器视图,第一响应者将退出,关闭键盘。我在使用UITableView、搜索栏和键盘时经常使用的另一种方法是,我认为您可以将其应用于文本字段
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO; // this prevents the gesture recognizers to 'block' touches
[gestureRecognizer release];
然后,这里是一个简单的回调函数
- (void)hideKeyboard {
[myTextField resignFirstResponder];
}
希望它能有所帮助我想要一个版本,可以从任何视图辞职,而不需要为每个视图指定具体的出口。。这是我的解决方案,我把它放在我的一个标准容器视图中 它使用的是MonoTouch C#,不过如何将其转换为Objective-C应该是显而易见的
private void findAndResignFirstResponder(UIView node=null) {
if (node == null) { return; }
if (node.IsFirstResponder) {
node.ResignFirstResponder();
return;
}
foreach (UIView view in node.Subviews) {
findAndResignFirstResponder(node:view);
}
}
private bool haveDrag = false;
public override void TouchesEnded(NSSet touches, UIEvent evt) {
if (!haveDrag) {
UITouch touch = (UITouch)evt.AllTouches.AnyObject;
if (!touch.View.CanBecomeFirstResponder) {
this.findAndResignFirstResponder(this);
Console.WriteLine("resign");
}
}
base.TouchesEnded (touches, evt);
}
public override void TouchesMoved(NSSet touches, UIEvent evt) {
haveDrag = true;
base.TouchesMoved (touches, evt);
}
public override void TouchesBegan(NSSet touches, UIEvent evt) {
haveDrag = false;
base.TouchesBegan (touches, evt);
}
这里有一个更好的解决方案,它不依赖于插座,并且可以处理控制器中任意数量的UITextField对象
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
我的方法是
- 在ViewController的@界面中添加对TextView的引用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if (keyboardShown && [touch view] != self.TextView) { [self.TextView resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; } - (void)keyboardWasShown:(NSNotification*)aNotification { keyboardShown = YES; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if ([myTextView isFirstResponder] && [touch view] != myTextView) { [myTextView resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; }
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if ([self.yourTextView isFirstResponder]) { [self.yourTextView resignFirstResponder]; } }
- 在ViewController的@implementation中重写此方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if (keyboardShown && [touch view] != self.TextView) { [self.TextView resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; } - (void)keyboardWasShown:(NSNotification*)aNotification { keyboardShown = YES; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if ([myTextView isFirstResponder] && [touch view] != myTextView) { [myTextView resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; }
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if ([self.yourTextView isFirstResponder]) { [self.yourTextView resignFirstResponder]; } }
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
这是一篇老文章,但我在实现这个解决方案时发现了一些更简单的东西(当时可能没有) [self.myTextView endEditing:是] 我使用的是UITableView,所以我将这一行放在didSelectRowatineXpath:方法上。 到目前为止,一切都很好
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch = event.allTouches()?.anyObject() as UITouch
if( textfield.isFirstResponder() && touch.view != textfield) {
textfield.resignFirstResponder()
NSLog("Resigning first responder since touches began outside")
}
super.touchesBegan(touches, withEvent: event)
}
奥霍洛布的回答对我也有用。这是简单的swift等价物 这是swift 3代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if textField.isFirstResponder && touch.view != textField {
textField.resignFirstResponder()
}
}
super.touchesBegan(touches, with: event)
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
如果textField.isFirstResponder&&touch.view!=textField{
textField.resignFirstResponder()辞职
}
}
super.touchesbeated(touches,with:event)
}
如何将touchesbearth方法添加到我已创建的UIViewController的self.view子视图的UITableView中。我是否必须将UITableView子类化,即@interface TouchableTableView:UITableView?或者,是否有一种更简单的方法来完成它,而不必子类化UITableView?如果这对他们不起作用,我很乐意帮助他们。。只需留下评论或开始一个新问题并在此处链接即可。ohhorob,如果我们点击任何其他考虑中的渠道,此解决方案不起作用,您能帮我吗?以防有人仍在查看此。。。我有一个问题,我的根视图的大部分被UIScrollView覆盖。如果我直接在UIView上点击边距,键盘将关闭,但如果我点击UIScrollView,键盘将不会关闭。如果使用ViewController,您可以添加到[self view]。不,它不会工作,表格和滚动视图有它们自己的交互。这个答案是最好的。当我将tapGestureRecognizer添加到self.view时,它也适用于滚动视图中的一组文本字段。这解决了问题。这里的关键是第三行,它允许其他触摸事件也发生(通过不阻塞)。它也适用于表视图。(在我的例子中,搜索栏位于表视图单元格内,这不允许有出口。在这种情况下,您可以执行self.view.endEditing(true)
而不是辞职FirstResponder。不要认为你需要检查UITextField-即使你不加区别地编辑:在文本字段之间切换仍然有效,键盘也不会启动