2011-07-05 49 views
5

我有多个自定义UIWebView作为UIScrollView内的子视图。我已经在UIWebView的UIViewController中实现了触摸方法,但是这些方法没有被调用。UIWebView里面的UIScrollView消费触摸事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { } 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { } 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } 

我已经读过其他地方,它说UIScrollView会消耗事件。这是有道理的,但是如果UIWebView在UIScrollView本身之前没有收到触摸事件?可能发生的事情是,UIWebBrowserView(UIWebView的子视图)正在消耗事件。然而,这个类不能被子类化,所以我不能把事件通过superview链发送到UIScrollView。我已阅读Apple iOS文档,认为不应该在UIS​​crollView中放置UIWebView,但我需要为我的目的这样做。

所以我的问题是:我的自定义UIWebView(即UIScrollView的子视图)如何接收触摸事件?

我的控制器和视图被设置如下:

HomeViewController.h /.m /.xib 
HomeView.h /.m (is a UIScrollView) 
DashboardViewController.h /.m /.xib (custom UIWebView's) 

HomeViewController从包含自定义视图中的笔尖文件中加载:HomeView。 DashboardViewController是从一个Nib文件加载的。它包含一些标签,滑块等(请注意,这里没有自定义视图 - 我没有在这里继承UIView)。

HomeViewController动态加载多个DashboardViewControllers并将其视图添加到HomeView。

DashboardViewController *dashboardViewController = [[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:[NSBundle mainBundle]; 
[homeView addSubview:dashboardViewController.view]; 

回答

0

用户交互标准启用。

您是否已将视图委托连接到文件所有者? 你是否将视图绑定到控制器?并确保将相应的操作连接到正确的方法。 (CTRL点击查看,并在您Class.h文件中的加号拖动到相应的方法。

@interface MyScrollView : UIViewController { 
    UIView *myFirstView; 
    UIView *mySecondView; 
    UIView *myThirdView; 
} 

@property (nonatomic, retain) IBOutlet UIView *myView; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

@end 
+0

是的,我的意见代表已连接到文件所有者。你是什​​么意思将我的视图绑定到控制器?另外,我连接了UIScrollView子类作为Controller的主视图。查看我更新的帖子;我添加了一些可能有所帮助的信息。谢谢。 – mdupls

+0

该解决方案不起作用,因为UIWebBrowserView(而不是UIWebView)似乎挑选了触摸事件。然而,这个类不能被改写为功能。 – mdupls

0

您是否为所有子视图设置了userInteractionEnabled = YES

+0

userInteractionEnabled默认启用。 – Jules

+0

糟糕。以为这是默认的NO。 scrollView上的contentSize也许? –

0

UIViewController中得到触摸*前的消息UIView.UIViewController默认没有将消息传递给UIView(通过调用[super touchesBegan]),所以如果你的UIView有一个控制器,你需要移动或转发触摸*函数。把各种触及视图子类中的方法,而不是视图控制器子类。

+0

这里的问题是,我没有为UIScrollView中的子视图的子视图继承UIView。我只是使用从Nib生成的视图。查看我更新的帖子;我添加了一些可能有所帮助的信息。谢谢。 – mdupls

0

你可以试试这个,当你为UIScrollView调用触摸方法时,你可以在子视图中从scrollview中调用相同的触摸方法。