如何将touchesBegan从UIScrollView传递到UIViewController
实际上,我想将数据从一个视图推送到另一个视图。我有一个 UIScrollView ,上面有图像。现在我想将数据从该视图推送到其他视图,因为每当我触摸该图像视图时,我想选择位置并将该位置推送到其他视图。 UIScrollView没有navigationController,无法推送它。
UIScrollView 位于 UIViewController 类之上。有没有办法将 UITouch 从 UIScrollView 发送到 UIViewController 类并让该类推送视图,或者我是否必须解决一些问题。
我有 UIScrollView 类的以下代码,
ImageScrollView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint touchInScrollView = [t locationInView:imageView];
NSLog(@"x: %f", floor(touchInScrollView.x));
NSLog(@"Y : %f", floor(touchInScrollView.y));
/****Cant be done since pushViewController is not allowed in UIScrollView.****/
DetailViewController *newView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
}
Actually, I want to push data from one view to other. I have a UIScrollView which has images on top of it. Now I want to push the data from that view to other view since whenever I do touch on that image view I want to select the position and push the location to other view.
UIScrollView does not have navigationController and cannot be able to push it.
UIScrollView is on top of UIViewController class. Is there a way to send the UITouch from UIScrollView to UIViewController Class and let that class push the view or do I have to work around some thing.
I have the following code for UIScrollView class,
ImageScrollView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint touchInScrollView = [t locationInView:imageView];
NSLog(@"x: %f", floor(touchInScrollView.x));
NSLog(@"Y : %f", floor(touchInScrollView.y));
/****Cant be done since pushViewController is not allowed in UIScrollView.****/
DetailViewController *newView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此属性添加到 ImageScrollView.h
在 ImageScrollView.m 中的 @implementation 之后添加此行
将此行添加到 ImageScrollView.m 中的 - (void)dealloc 方法
如果您以编程方式创建 ImageScrollView,则在视图控制器中创建后:
如果您创建Interface Builder 中的 ImageScrollView,将 ParentVC 出口连接到 File's Owner。
要在 ImageScrollView.m 中推送新的视图控制器,
这里有一个替代方案,
如果不了解有关您的应用程序的更多信息,我不能肯定地说,但一般来说,如果我有一个包含我想要点击的图像的滚动视图,我会使用UIButton 而不是 UIImageView,并执行类似的操作
如果您不以编程方式创建按钮,您还可以通过将属性检查器中的类型更改为自定义来将图像设置为背景,并将背景属性更改为所需的图像。
使用按钮而不是图像视图将允许您将按钮的 touchDown 出口连接到主视图控制器中的 IBAction 方法,该方法可能如下所示:
Add this property to ImageScrollView.h
Add this line after @implementation in ImageScrollView.m
Add this line to your - (void)dealloc method in ImageScrollView.m
If you create your ImageScrollView programmatically, after creation in your view controller:
If you create your ImageScrollView in Interface Builder, connect the parentVC outlet to File's Owner.
To push a new view controller, in ImageScrollView.m
Here's an alternative,
I can't say for sure without knowing more about your application, but in general if I had a scroll view containing images that I wanted to tap, I would use a UIButton instead of UIImageView, and do something like
If you aren't creating buttons programmatically, you can also set an image as it's background by changing the Type in the Attributes inspector to Custom, and change the Background attribute to the desired image.
Using a button instead of an image view will allow you to connect the touchDown outlet of the button to an IBAction method in your primary view controller that might look like: