2009-08-06 64 views
2

我有一个UISegmentedControl,如果你点击已经选择的项目,我想用它来执行某个动作。我可以重写UISegmentedControl的UIControlEventTouchUpInside吗?

我的想法是基本上是这样的:

- (void)viewDidLoad { 
    UISegmentedControl * testButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"one", @"two", nil]]; 
    [self.view addSubview:testButton]; 
    [testButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [super viewDidLoad]; 
} 

-(void) clicked: (id) sender{ 
    NSLog(@"click"); 
} 

(在clicked:我只是做一些检查,以查看新选定的索引点击之前与旧选择指数不同)

问题是我似乎无法重写TouchUpInside控件事件的操作。任何帮助感谢!

-S

回答

5

你可以使用一个子类来获得你想要的行为。让UISegmentedControl的子类,有一个BOOL伊娃:

BOOL _actionSent; 

然后,在实施,覆盖以下两种方法:

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { 
    [super sendAction:action to:target forEvent:event]; 
    _actionSent = TRUE; 
} 

- (void) setSelectedSegmentIndex:(NSInteger)toValue { 
    _actionSent = FALSE; 

    [super setSelectedSegmentIndex:toValue]; 

    if (!_actionSent) { 
     [self sendActionsForControlEvents:UIControlEventValueChanged]; 
     _actionSent = TRUE; 
    } 
} 

可能有其他的方法,但这个工作确定为我。我有兴趣了解其他方法。

-1

使用UIControlEventValueChanged与UISegmentedControls。

+0

问题是如果值已经改变,那么显然用户还没有触及已经选择的项目。 – spencewah 2009-08-07 05:46:59

+0

同意这不会回答你的问题,但它确实帮助我,所以我给了它一个投票。 – lambmj 2012-01-19 14:28:50

相关问题