Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:14:05+00:00 2026-06-08T15:14:05+00:00

How do I get a reference to the UIViewController of a touched view? I

  • 0

How do I get a reference to the UIViewController of a touched view?

I am using a UIPanGestureRecognizer on the view of a UIViewController. Here’s how I initialize it:

    TaskUIViewController *thisTaskController = [[TaskUIViewController alloc]init];
    [[self view]addSubview:[thisTaskController view]];
    UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [[thisTaskController view] addGestureRecognizer:panRec];

In the tiggered action triggered using the gesture recognizer I am able to get the view from the parameter using recognizer.view

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    UIView *touchedView = [[UIView alloc]init];
    touchedView = (UIView*)[recognizer view];
    ...
}

However what I really need is the underlying UIViewController of the view touched. How can I get a reference to the UIViewController that contains this view instead of only the UIView?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:14:06+00:00Added an answer on June 8, 2026 at 3:14 pm

    I would say that it is more a design issue than just getting a reference. So I would follow several simple advises:

    1. Owner should catch events from its view. I.e. TaskUIViewController sould be a target to UIPanGestureRecognizer which you added to its view.
    2. If a controller has a sub-controller and waits from its sub-controller some responses – implement this as delegate.
    3. You have memory leak in your “handlePan:” method.

    Here is a skeleton to solve your issue:

    @protocol CallbackFromMySubcontroller <NSObject>
    - (void)calbackFromTaskUIViewControllerOnPanGesture:(UIViewController*)fromController;
    @end
    
    @interface OwnerController : UIViewController <CallbackFromMySubcontroller>
    @end
    
    @implementation OwnerController
    
    - (id)init
    {
        ...
        TaskUIViewController *thisTaskController = [[TaskUIViewController alloc] init];
        ...
    }
    
    - (void)viewDidLoad
    {
        ...
       [self.view addSubview:thisTaskController.view];
        ...
    }
    
    - (void)calbackFromTaskUIViewControllerOnPanGesture:(UIViewController*)fromController
    {
        NSLog(@"Yahoo. I got an event from my subController's view");
    }
    
    @end
    
    
    @interface TaskUIViewController : UIViewController {
        id <CallbackFromMySubcontroller> delegate;
    }
    @end
    
    @implementation TaskUIViewController
    
    - (id)initWithOwner:(id<CallbackFromMySubcontroller>)owner
    {
        ...
        delegate = owner;
        ...
    }
    
    - (void)viewDidLoad
    {
        UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [self.view addGestureRecognizer:panRec];
        [panRec release];
    }
    
    - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
        ...
        [delegate calbackFromTaskUIViewControllerOnPanGesture:self];
        ...
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to get a reference to the object whose view was touched on
I hope to access UIAlertView using tag. The codes show below UIAlertView *a=(UIAlertView *)[self.view
I want to get reference on BaseController from AppDelegate. My MainMenu.xib's hierarchy is where
How can I get a reference to the directory of the visual studio project
How can I get a reference to a module from within that module? Also,
How do I get Assembly reference for a .dll with no types in it?
I want to get a reference to the Gmail Inbox button in Javascript? I
I'd like to get a reference CMAttitude based on the ground level, for instance,
I'm trying to get a reference to cell and it appears null. If I'm
Is it possible to get a reference to the triggerElement that invoked the Ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.