lifemoveson
lifemoveson

Reputation: 1691

how to pass touchesBegan from UIScrollView to UIViewController

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];

}

Upvotes: 1

Views: 1610

Answers (1)

Jacob Jennings
Jacob Jennings

Reputation: 2836

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;

Upvotes: 1

Related Questions