2011-11-13 29 views
1

我正在使用UIBarButtonItem来触发事件。我在xcode4中使用集成的InterfaceBuider来创建我的UIBarButtonItem,然后将按钮连接到我的视图控制器中的一个方法。该方法是这样的:如何检测UIBarButtonItem的touchesbegan touchesended?

-(IBAction)NoteOnOff:(id)sender 
{ 
    UIButton *button = (UIButton*)sender; 

    /* now perform action */ 
} 

现在我也想检测fingerdown/fingerup因为我想触发noteon/noteoff类型的事件的MIDI合成器一类应用的。

1-有没有方法可以检测sender在上述方法中是按下了还是按下了?

2 - 我想继承的UIBarButtonItem和实施的touchesBegan和touchesEnded这样:

@interface myUIBarButtonItem : UIBarButtonItem { 

} 

@end 

@implementation myUIBarButtonItem 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
NSLog(@"touchesBegan"); 
NSLog(@"touches=%@,event=%@",touches,event); 
} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
NSLog(@"touchesEnded"); 
NSLog(@"touches=%@,event=%@",touches,event); 
} 

@end 

然后,我改变了类的UIBarButtonItem的界面编辑器,但没有运气myUIBarButtonItem。这是在界面编辑器中使用我的自定义类的正确方法吗?

3我从某处读取UIBarButtonItem不从UIResponder继承,因此它们无法拦截touchesbegan/touchesended事件。如果是这种情况,那么能够检测触及并触发事件的正确方法是什么?我主要是C/C++程序员,而且我的知识对于目标C和iphone环境非常有限。我只知道如何使用UI编辑器,我不知道如何创建自定义UI并在没有此编辑器的情况下使用它们。

底线是:什么是最简单的方式来检测touchdown/touchup与最小的延迟可能?

任何指向教程或文档的指针也是受欢迎的。

感谢,

巴巴

回答

-2

你可以像下面这样做(没有需要继承的UIBarButtonItem):

[button addTarget:self action:@selector(touchUp:) 
    forControlEvents:UIControlEventTouchUpInside]; 

[button addTarget:self action:@selector(touchDown:) 
    forControlEvents:UIControlEventTouchDown]; 

- (void) touchUp:(id)sender { 
} 

- (void) touchDown:(id)sender { 
} 
+0

感谢迈克尔,这看起来像正是我需要的。有一件事是,我不知道如何访问'button',因为它是在IB中创建的。 – Baba

+0

我试图在我的viewcontroller.h中这样声明一个IBOutlet:IBOutlet UIBarButtonItem * myButton;然后链接到IB的代码行,然后在viewdidload中调用[myButton addTarget ....],但是我得到的运行时错误如下所示: - [UIBarButtonItem addTarget:action:forControlEvents:]:无法识别的选择器发送到实例 ***由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:' - [UIBarButtonItem addTarget:action:forControlEvents:]:无法识别的选择器发送到实例 ***一次调用堆栈: 我失踪了什么?谢谢 – Baba

+5

这是因为addTarget:action:forControlEvents:在UIControl中声明,而UIBarButtonItem不从UIControl继承。 – NJones

相关问题