我在我的 UITableViewController 子类中定义了这个方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touch began");
}
但它没有被触发。UIViewController 是 UIResponder 的子类,所以它应该可以工作,对吧?谢谢
我在我的 UITableViewController 子类中定义了这个方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touch began");
}
但它没有被触发。UIViewController 是 UIResponder 的子类,所以它应该可以工作,对吧?谢谢
如果您试图关闭 UITableViewController 中的键盘,一个不错的解决方案是在 tableView 委托的 didSelectRowAtIndexPath 方法中调用 resignFirstResponder。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[myTextField resignFirstResponder];
// Rest of your actual didSelectRowAtIndexPath code ...
}
是的,那肯定会奏效。
确保为此控制器对应的视图设置了用户交互启用。
如果视图是从 nib/xib 加载的,请确保将 File's Owner 设置为适当的类名,并且 File's Owner 的view
出口连接到正确的视图。
更新:我也看到了这种行为,使用基于导航的应用程序模板构建的应用程序,但使用基于视图的应用程序模板,它可以按预期工作。
我认为在导航控制器的情况下,嵌入在视图控制器中的表视图在视图控制器之前获取事件。请参阅UIView 参考(重点是我的):
视图控制器本身是 UIResponder 类的后代,并被插入到托管根视图与其父视图之间的响应者链中,通常属于不同的视图控制器。如果视图控制器的视图不处理事件,则视图控制器本身可以选择在将事件传递给父视图之前处理事件。
这意味着视图首先有机会处理该事件,如果这样做了,视图控制器将不会得到它。
不完全清楚您要完成什么,所以我不确定要提供什么解决方案。
我刚刚遇到了与原始帖子一样的确切问题。我通过创建 UITableView 的子类并覆盖“touchesBegan”方法解决了这个问题。我还创建了一个协议,以便 UITableView 可以调用 UITableViewController 上的方法,这最终是我想要处理“touchesBegan”事件的地方。
这是 UITableView 子类型的标题:
@protocol AGSTableViewDelegate;
@interface AGSTableView : UITableView
@property (nonatomic, weak) id<AGSTableViewDelegate> agsDelegate;
@end
@protocol AGSTableViewDelegate<NSObject>
-(void)tableViewTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
和实施:
@implementation AGSTableView
@synthesize agsDelegate;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if (agsDelegate)
[agsDelegate tableViewTouchesBegan:touches withEvent:event];
}
@end
最后,来自 UITableViewController 的代码片段:
@interface AGSWasteUpdateTableController ()<AGSTableViewDelegate>
@end
- (void)viewDidLoad
{
AGSTableView *tableView = (AGSTableView *)[self tableView];
tableView.agsDelegate = self;
}
-(void)tableViewTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[yourTextField resignFirstResponder];
}
希望这可以帮助...
Swift 版本,因为我刚刚遇到了这个问题:
import UIKit
class BubbleTouchesTableView: UITableView {
// Designed to bubble up touches from a UITableView
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
self.nextResponder()?.touchesBegan(touches, withEvent: event)
}
override func touchesCancelled(touches: NSSet, withEvent event: UIEvent) {
super.touchesCancelled(touches, withEvent: event)
self.nextResponder()?.touchesCancelled(touches, withEvent: event)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
self.nextResponder()?.touchesEnded(touches, withEvent: event)
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
self.nextResponder()?.touchesMoved(touches, withEvent: event)
}
}
您需要将触摸传递到响应者链。我们可以通过继承 UITableView 并覆盖 touchesBegan(以及其他如果需要),然后将响应链向上传递到 UITableViewController 来做到这一点。
UITableViewTouch.h:
#import <UIKit/UIKit.h>
@interface UITableViewTouch : UITableView
@end
UITableViewTouch.m:
#import "UITableViewTouch.h"
@implementation UITableViewTouch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[[self nextResponder] touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[[self nextResponder] touchesCancelled:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[[self nextResponder] touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[[self nextResponder] touchesMoved:touches withEvent:event];
}
@end