gpt4 book ai didi

iphone - 如何让键盘消失?

转载 作者:行者123 更新时间:2023-12-03 19:46:35 25 4
gpt4 key购买 nike

我有一个显示在 UITableViewCell 中的文本字段,我希望当用户触摸屏幕上除文本字段之外的任何其他位置时能够隐藏键盘。我知道 [field resignFirstResponder];,但我不知道如何拦截 UITableView 背景上的触摸以调用“resignFirstResponder”。

任何帮助将不胜感激。

最佳答案

执行此操作的唯一方法是子类化 UITableView,在子类化的 UITableView 中实现 TouchBegan 方法,并将 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

然后在您将分配 UITableView 的常规文件中,只需分配子类 View 并将文本字段传递给子类。希望有帮助。

关于iphone - 如何让键盘消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3441444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com