我正在组合Swift代码和第三方库(用Obj-C编写)。我有一个带有UISegmentedController的UIViewController,我想在每次推送一个段时触发,或者再次推送同一段。
在我的Swift代码中,我有以下内容:
override func viewDidLoad() {
super.viewDidLoad()
//setup
items = ["newTab".localized,"topTab".localized,"categoryTab".localized]
carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: items as [AnyObject], delegate: self)
carbonTabSwipeNavigation.insertIntoRootViewController(self)
self.style()
self.view.userInteractionEnabled = true
carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self, action: #selector(OverviewFolder.changesMade), forControlEvents: UIControlEvents.ValueChanged)
}
func changesMade() {
switch carbonTabSwipeNavigation.carbonSegmentedControl!.selectedSegmentIndex {
case 0:
print("tab 1")
case 1:
print("tab 2")
case 2:
print("tab 3")
default:
print("nope")
}
}
在库中我添加了以下代码:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSInteger current = self.selectedSegmentIndex;
[super touchesEnded:touches withEvent:event];
if (current == self.selectedSegmentIndex)
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
所以基本上我想在每次用户按下一个段时触发一个ValueChanged动作(即使它是同一个段)。目前,当我按下相同的段时,它会第二次触发,但之后UISegmentController变得无响应(无法再切换段)。
答案 0 :(得分:3)
最终对我有用的是:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
和
carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self, action: #selector(OverviewFolder.changesMade), forControlEvents: UIControlEvents.TouchUpInside)
答案 1 :(得分:2)
你应该使用touchesEnded函数,当用户从屏幕上移开手指并使用sendActions时调用该函数:
目标-C
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
夫特
override func touchesEnded(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
self.sendActions(for: .touchUpInside)
}