我正在开发一个OpenSource Magazine Engine,您可以在GitHub上查看:
https://github.com/interactivenyc/Defrag
我在UIView中设置了一个名为MenuPanel的UIToolbar。由于某种原因,UIToolbar中的UIBarButtonItems没有正确调用它们的动作。这是我用于按钮的语法:
UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
正在发生的事情是,无论我在屏幕上点击的任何地方,都会调用在我的主UIViewController中声明的UITapGestureRecognizer。这可以在我的主UIViewController中的这段代码中进行设置:
- (void)setupGestureRecognizers {
//NSLog(@"setupGestureRecognizer NEW");
UISwipeGestureRecognizer *swipeRecognizer;
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
}
我确信我在尝试这样做时有一些非常基本的概念错误。有人能告诉我如何解决这个问题吗?
作为参考,你可以在这里看到我的主要DefragViewController:UIViewController:
https://gist.github.com/1431722
我的MenuPanel:UIView在这里:
gist.github.com/1431728
答案 0 :(得分:5)
我解决了自己的问题。
我不得不告诉我的UIViewController忽略来自任何UIToolbar的触摸:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//ignore any touches from a UIToolbar
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}