我可以为UISegmentedControl覆盖UIControlEventTouchUpInside吗? [英] Can I override the UIControlEventTouchUpInside for a UISegmentedControl?

查看:83
本文介绍了我可以为UISegmentedControl覆盖UIControlEventTouchUpInside吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UISegmentedControl,如果您单击已选择的项目,我想使用该UISegmentedControl来执行特定的操作.

I have a UISegmentedControl which I'd like to use to perform a certain action if you click the already selected item.

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

My thinking is basically something like this:

- (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:中,我将做一些检查,以查看单击之前新选择的索引是否不同于旧选择的索引)

(And in clicked: I'd just do some check to see if the new selected index is different from the old selected index before the click)

问题是我似乎无法覆盖TouchUpInside控件事件的操作.任何帮助表示赞赏!

The problem is I can't seem to override the action for the TouchUpInside control event. Any help appreciated!

-S

推荐答案

您可以使用子类来获取所需的行为.制作具有一个BOOL ivar的UISegmentedControl的子类:

You can use a subclass to get the behavior you want. Make a subclass of UISegmentedControl that has one BOOL ivar:

BOOL _actionSent;

然后,在实现中,重写以下两个方法:

Then, in the implementation, override the following two methods:

- (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;
    }
}

可能还有其他方法,但这对我来说还行.我很想学习其他方法.

There are probably other ways but this worked ok for me. I'd be interested to learn of other approaches.

这篇关于我可以为UISegmentedControl覆盖UIControlEventTouchUpInside吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆