我正在使用自定义手势识别器(如链接Intercepting/Hijacking iPhone Touch Events for MKMapView中所示)来检测MKMapView上的触摸事件。手势识别器在WildcardGestureRecognizer.h中定义,并在WildcardGestureRecognizer.m文件中实现。当此手势识别器添加到MKMapview时,可以从以下方法中读取任何触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (touchesBeganCallback)
touchesBeganCallback(touches, event);
NSLog(@"touchesBegan");
}
基于此触摸检测,我想从MainViewController(包含MKMapView)调用方法tapMethod。
-(void) tapMethod
{
dontUpdateLocation =1;// a variable to check stoppoing of location update.
NSLog(@" map tapped");
}
我试过了
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (touchesBeganCallback)
touchesBeganCallback(touches, event);
NSLog(@"touchesBegan");
MainViewController *updateController = [[MainViewController alloc]init ];
[updateController tapMethod];
[updateController release];
}
打印“地图点击”但不改变变量dontUpdateLocation的值。 我该怎么办?
答案 0 :(得分:1)
根据我的评论理解,我认为问题是由于你这样做的事实:
MainViewController *updateController = [[MainViewController alloc]init ];
[updateController tapMethod];
[updateController release];
您没有创建对现有mainviewcontroller的引用,但是您正在创建一个指向内存中另一个对象的不同指针。 您可以使用appDelegate存储(设置@property和@synthesize)变量,然后像这样访问:
YourAppDelegate *appDel=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDel.dontUpdateLocation=1;
我建议你仔细研究一下这些模式:Singletons,MVC和Delegation
希望这有帮助。