1

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]);

        }

    }
}
}
4

1 回答 1

1

好的,最后我想出了如何获取更新的值。

覆盖 sendAction 而不是 sendEvent。在这里,您可以获得事件类型以及 selectedSegmentIndex 的更新值。

-(BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
NSLog(@"%u",[((UIControl*)sender) allControlEvents]);

}
于 2013-08-21T08:22:29.340 回答