UISegmentedController 在按同一段两次后冻结
UISegmentedController freezes after pressing same segment twice
我正在组合 Swift 代码和第三方库(用 Obj-C 编写)。我有一个带有 u 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 变得没有响应(不能再切换段)。
最终对我有用的是:
- (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)
您应该使用 touchesEnded 函数,当用户从屏幕上移开手指并使用 sendActions 时调用该函数:
Objective-C
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Swift
override func touchesEnded(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
self.sendActions(for: .touchUpInside)
}
我正在组合 Swift 代码和第三方库(用 Obj-C 编写)。我有一个带有 u 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 变得没有响应(不能再切换段)。
最终对我有用的是:
- (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)
您应该使用 touchesEnded 函数,当用户从屏幕上移开手指并使用 sendActions 时调用该函数:
Objective-C
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Swift
override func touchesEnded(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
self.sendActions(for: .touchUpInside)
}