我有一个UITextView,用于让用户提交一些文本。
我的问题是,我似乎无法想出如何让用户通过点击UITextView外部来“取消”。
我有一个UITextView,用于让用户提交一些文本。
我的问题是,我似乎无法想出如何让用户通过点击UITextView外部来“取消”。
简化自tuzzolotron的回答: 在您的xib中正确连接以下outlet
IBOutlet UITextView *myTextView;
在视图控制器中使用以下代码:
- (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];
}
点击视图控制器视图上文本视图之外的任何位置,会注销第一响应者并关闭键盘。
我在使用UITableView、Searchbar和键盘时经常采用另一种方法,我认为你可以将其应用到文本字段中。
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];
}
希望它能帮到你
self.view.endEditing(true)
而不是 resignFirstResponder。 - Seop Yoon在我看来:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// pass touches up to viewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (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;
}
这是一篇旧文章,但当我在实现这个解决方案时,我发现有一个更简单的方法(也许当时还没有)。
[self.myTextView endEditing:YES];
我正在使用UITableView,所以我把这行代码放在didSelectRowAtIndexPath:方法中。目前为止,一切都很好...
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)
}
ohhorob的答案对我也有用。这是简单的Swift等效代码。
- (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];
}
endEditing:
,在文本字段之间切换仍然有效,键盘也会保持弹出。 - Nas Banov这里是 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)
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
我想要一个可以在任何视图中重新签名而不需要为每个视图指定特定输出的版本。这是我的解决方案,我将其放入了我的标准容器视图之一。
它使用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);
}
我的做法是...
在您的ViewController的@interface中添加对TextView的引用:
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
在您的ViewController的@implementation中重写此方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([self.yourTextView isFirstResponder]) {
[self.yourTextView resignFirstResponder];
}
}