简体   繁体   English

如何检测已选择的UIBarButtonItem的触摸?

[英]how can i detect touch on already selected UIBarButtonItem?

how can i detect touch on already selected UIBarButtonItem? 如何检测已选择的UIBarButtonItem的触摸? if i touch UIBarButtonItem , it will be selected..but if i touch the same again, how can i identify it ? 如果我触摸UIBarButtonItem,它将被选中..但是如果我再次触摸相同的按钮,我如何识别它? any help please? 有什么帮助吗?

the code is as follows.. initWithCustomView:sortToggle in which sortToggle is segmented control... 代码如下。initWithCustomView:sortToggle其中sortToggle是分段控件...

 UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc] 
                                         initWithCustomView:sortToggle];
 [sortToggle release];

 UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] 
                  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                       target:nil
                                       action:nil];

 NSArray *buttonArray = [NSArrayarrayWithObjects:flexibleSpaceButtonItem, 
                                                 sortToggleButtonItem, 
                                                 flexibleSpaceButtonItem, nil];

 [self setToolbarItems: buttonArray animated:YES];

The UIBarButtonItem does not inherit from the UIResponder so you can not get touchesBegan / touchedEnded etc events directly. UIBarButtonItem不继承自UIResponder,因此您无法直接获取touchesBegan / touchedEnded等事件。 If you set the UISegmentedControl to be momentary ( yourSegControl.momentary = YES ) you can select a button more than once. 如果将UISegmentedControl设置为瞬时的( yourSegControl.momentary = YES ),则可以多次选择一个按钮。 Is this helpful for your situation? 这对您的情况有帮助吗?

Otherwise, you should probably subclass the UISegmentedControl (which inherits from the UIResponder) and handle the extra touches yourself (don't forget to call super in any touch method you override). 否则,您可能应该子类化UISegmentedControl(继承自UIResponder)并自己处理多余的触摸(不要忘记在您覆盖的任何触摸方法中调用super)。

I needed to be able to detect when the user selected an already selected index, and I found the problem was that the UISegmentedControl only fired the UIControlEventValueChanged event. 我需要能够检测用户何时选择了一个已选择的索引,但我发现问题是UISegmentedControl仅触发了UIControlEventValueChanged事件。 I wanted to be able to get the UIControlEventTouchUpInside event so I could do something. 我希望能够获得UIControlEventTouchUpUpInside事件,因此我可以做一些事情。

The solution is to subclass UISegmentedControl and overwrite the touch event to fire the event you want. 解决方案是子类化UISegmentedControl并覆盖touch事件以触发您想要的事件。

Create a subclass of UISegmentedControl and override touchesBegan:withEvent: ... 创建UISegmentedControl的子类并覆盖touchesBegan:withEvent: ...

/* MySegmentedControl.m, a subclass of UISegmentedControl... */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  //dont forget to call super!
  [super touchesBegan:touches withEvent:event];
  //fire the event we want to listen to
  [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

Then listen for the UIControlEventTouchUpInside of your segmented control instead of UIControlEventValueChanged so that you only get one event fired...otherwise it will fire the UIControlEventValueChanged event once naturally, and once when you fire it, and you'll get two events (which you obviously don't want). 然后听你的分段控制,而不是UIControlEventValueChangedUIControlEventTouchUpInside让你只能得到一个触发的事件......否则,当你火它,它就会自然地一次触发事件UIControlEventValueChanged,有一次,你会得到两个事件(你显然不想要)。

[mySegmentedControlInstance addTarget:self action:@selector(mySegmentedControlSelected:) forControlEvents:UIControlEventTouchUpInside]

This way you get an event once every time the control is touched, not just when it's value is changed. 这样,您每次触摸控件时都会获得一次事件,而不仅仅是更改其值时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM