如何将touchesBegan从UIScrollView传递到UIViewController

发布于 2024-12-05 04:30:16 字数 895 浏览 0 评论 0原文

实际上,我想将数据从一个视图推送到另一个视图。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-12-12 04:30:16

将此属性添加到 ImageScrollView.h

@property (nonatomic, retain) IBOutlet UIViewController* parentVC;

在 ImageScrollView.m 中的 @implementation 之后添加此行

@synthesize parentVC;

将此行添加到 ImageScrollView.m 中的 - (void)dealloc 方法

[parentVC release];

如果您以编程方式创建 ImageScrollView,则在视图控制器中创建后:

imageScrollViewYouJustMade.parentVC = self;

如果您创建Interface Builder 中的 ImageScrollView,将 ParentVC 出口连接到 File's Owner。

要在 ImageScrollView.m 中推送新的视图控制器,

[self.parentVC.navigationController pushViewController:newView animated:YES];

这里有一个替代方案,

如果不了解有关您的应用程序的更多信息,我不能肯定地说,但一般来说,如果我有一个包含我想要点击的图像的滚动视图,我会使用UIButton 而不是 UIImageView,并执行类似的操作

btnImage = [UIImage imageNamed:@"image.png"];
[btn setImage:btnImage forState:UIControlStateNormal];

如果您不以编程方式创建按钮,您还可以通过将属性检查器中的类型更改为自定义来将图像设置为背景,并将背景属性更改为所需的图像。

使用按钮而不是图像视图将允许您将按钮的 touchDown 出口连接到主视图控制器中的 IBAction 方法,该方法可能如下所示:

- (IBAction)smileyFaceButtonTapped:(UIButton*)sender;

Add this property to ImageScrollView.h

@property (nonatomic, retain) IBOutlet UIViewController* parentVC;

Add this line after @implementation in ImageScrollView.m

@synthesize parentVC;

Add this line to your - (void)dealloc method in ImageScrollView.m

[parentVC release];

If you create your ImageScrollView programmatically, after creation in your view controller:

imageScrollViewYouJustMade.parentVC = self;

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

[self.parentVC.navigationController pushViewController:newView animated:YES];

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

btnImage = [UIImage imageNamed:@"image.png"];
[btn setImage:btnImage forState:UIControlStateNormal];

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:

- (IBAction)smileyFaceButtonTapped:(UIButton*)sender;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文