Reputation:
how can i detect touch on already selected UIBarButtonItem? if i touch UIBarButtonItem , it will be selected..but if i touch the same again, how can i identify it ? any help please?
the code is as follows.. initWithCustomView:sortToggle in which sortToggle is segmented control...
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];
Upvotes: 1
Views: 1276
Reputation: 2563
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. I wanted to be able to get the UIControlEventTouchUpInside event so I could do something.
The solution is to subclass UISegmentedControl and overwrite the touch event to fire the event you want.
Create a subclass of UISegmentedControl and override 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).
[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.
Upvotes: 0
Reputation: 13670
The UIBarButtonItem does not inherit from the UIResponder so you can not get touchesBegan / touchedEnded etc events directly. If you set the UISegmentedControl to be momentary (yourSegControl.momentary = YES
) you can select a button more than once. 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).
Upvotes: 1