gpt4 book ai didi

iphone - 我可以覆盖 UIControlEventTouchUpInside 的 UISegmentedControl 吗?

转载 作者:行者123 更新时间:2023-12-03 20:50:07 24 4
gpt4 key购买 nike

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

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

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

最佳答案

您可以使用子类来获得您想要的行为。创建一个具有一个 BOOL ivar 的 UISegmentedControl 子类:

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

可能还有其他方法,但这对我来说效果很好。我有兴趣了解其他方法。

关于iphone - 我可以覆盖 UIControlEventTouchUpInside 的 UISegmentedControl 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com