2016-11-10 36 views
5

我結合銀行代碼和第三方庫(寫的OBJ-C)後凍結。我有一個UIViewController用U UISegmentedController中,我想在同一網段再次推段已推每次或觸發。UISegmentedController按同一網段兩次

在我的銀行代碼,我有以下幾點:

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

的方法是這樣的內容:'#selector(OverviewFolder.changeImageView)'? –

+0

@AlexanderDoloz對不起,這是一個複製/粘貼錯誤,我改變了它。應該已經改變了(在代碼中可以) – SoundShock

回答

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