我有一个 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/