Reputation: 2078
I am in UIApplication::sendEvent override. I get a UIEvent which ultimately gives me UITouch object. When UITouch has phase == 3 (touch phase ended or touch up), I try to identify if the UITouch's view object was UISegmentedControl or not. If yes, I try to get selectedSegmentIndex. At this point and time I always get wrong value(or previous value of selectedSegmentIndex).
Is it that, this value is changed only after its action method is executed? Or What do I need to do to get the latest value of selectedSegmentIndex.
Any help appreciated.
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
NSSet *touches = [event allTouches];
NSEnumerator *enumerator = [touches objectEnumerator];
id value;
while ((value = [enumerator nextObject])) {
UITouch *touch = value;
if (touch.phase==3) {
if([touch.view isKindOfClass:[UISegmentedControl class]])
{
UISegmentedControl *sc = (UISegmentedControl*)touch.view;
NSLog(@"%d",[sc selectedSegmentIndex]);
}
}
}
}
Upvotes: 1
Views: 228
Reputation: 2078
Ok finally I figured how to get the updated value.
Override sendAction instead of sendEvent. Here you can get the event type as well as updated value for selectedSegmentIndex.
-(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
NSLog(@"%u",[((UIControl*)sender) allControlEvents]);
}
Upvotes: 1