chatur
chatur

Reputation: 2363

Calling a method in MainViewController from custom class file

I am using a custom gesture recognizer (as given in the link Intercepting/Hijacking iPhone Touch Events for MKMapView) for detecting touch events on MKMapView. gesture recognizer is defined in WildcardGestureRecognizer.h and implemented in WildcardGestureRecognizer.m file. When this gesture recognizer is added to MKMapview, one can read from following method any touch events

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (touchesBeganCallback)
        touchesBeganCallback(touches, event);
    NSLog(@"touchesBegan");


}

Based on this touch detection I want to call method tapMethod from MainViewController(Containing MKMapView).

-(void) tapMethod
{
    dontUpdateLocation =1;// a variable to check stoppoing of location update.
    NSLog(@" map tapped");
}

I tried following

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (touchesBeganCallback)
            touchesBeganCallback(touches, event);
        NSLog(@"touchesBegan");
        MainViewController *updateController = [[MainViewController alloc]init ];
        [updateController tapMethod];
        [updateController release];

    }

It does print "map tapped" but doesn't change value of variable dontUpdateLocation. How can I do it?

Upvotes: 1

Views: 324

Answers (1)

Mat
Mat

Reputation: 7633

From what I understand from your comments, I think the problem is due to the fact that when you do this:

MainViewController *updateController = [[MainViewController alloc]init ];
[updateController tapMethod];
[updateController release];

you're not creating a reference to the existing mainviewcontroller, but you are creating a different pointer that points to another object in memory. You may use the appDelegate to store(set @property, and @synthesize) the variable and then access like this:

YourAppDelegate *appDel=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDel.dontUpdateLocation=1;

I suggest you take a look and depth of these patterns: Singletons, MVC, and Delegation

Hope this helps.

Upvotes: 1

Related Questions