我结合
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变得无响应(无法再切换段).
解决方法
最终对我有用的是:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self sendActionsForControlEvents:UIControlEventTouchUpInside]; }
和
carbonTabSwipeNavigation.carbonSegmentedControl!.addTarget(self,forControlEvents: UIControlEvents.TouchUpInside)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。