【问题标题】:How to correctly detect touches in my UIView?如何正确检测我的 UIView 中的触摸?
【发布时间】:2010-02-23 10:59:53
【问题描述】:

我遇到了问题。我有一个 UIView,看起来像这样:

在那个视图控制器中,我实现了“touchesBegan:withEvent:”方法,但只有当我触摸视图底部的栏时才会触发该方法,当我触摸桌子时没有任何反应。

我怎样才能改变这种行为,以便能够检测到表格中发生的触摸?

这是基本上声明 UIViewController 的代码:

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface TripSearchDetailViewController : UIViewController 
{
    //Parent of this sub-view.
    UIViewController    *parentController;

    //GUI elements
    UITableView         *tableView;
    UIButton            *backButton;
}

//Button actions
- (IBAction) goBack:(id) sender;
- (IBAction) showDatePicker:(id) sender;

//Class Methods.
- (void) presentDatePicker;


@property (nonatomic, retain) UIViewController      *parentController;
@property (nonatomic, retain) IBOutlet UITableView  *tableView;
@property (nonatomic, retain) IBOutlet UIButton     *backButton;

@end

【问题讨论】:

    标签: iphone cocoa-touch uiview uiviewcontroller


    【解决方案1】:

    看起来你有一个 UITableViewController,而不是 UIViewController。您的表格视图在触摸到您的视图控制器之前拦截和处理触摸。您需要继承 UITableView,覆盖它的 -touchesBegan:withEvent: 方法,然后创建一个标准 UIViewController,将您的新子类添加到视图层次结构中。

    【讨论】:

    • @Ben:我很确定这个类是一个 UIViewController(编辑了我的帖子以添加这个类的@interface 的代码)。虽然,我添加了 UITableView 和界面生成器(不是以编程方式),也许这可能是问题?
    • 如果它是 UIViewController,那么只需将 UITableView 替换为 UITableView 的自定义子类,该子类覆盖 -touchesBegan:withEvent:。
    【解决方案2】:

    我认为在您的 TripSearchDetailViewController 中实现方法 hitTest:withEvent: 可能会有所帮助。如果你只是在这个方法中返回 true,你的触摸应该被识别。 另见:http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 2014-06-21
      相关资源
      最近更新 更多