2010-10-22 47 views

回答

5

我發現的關鍵是UISegmentedControl的子類。在正常情況下,UISegmentedControl根本不會響應UIControlEventTouchUpInside。通過繼承UISegmentedControl,你可以重新定義功能touchesEnded(基本上是當用戶觸摸的物體是什麼,都會激發)這樣:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesEnded:touches withEvent:event]; 
    [self.superview touchesEnded:touches withEvent:event]; 

    MyViewController *vc; // the view controller which contains the UISegmentedControl 
    for (UIView* next = [self superview]; next; next = next.superview) { 
     UIResponder* nextResponder = [next nextResponder]; 
     if ([nextResponder isKindOfClass:[MyViewController class]]) { 
      vc = (MyViewController*)nextResponder; 
      break; 
     } 
    } 

    if (vc) { 
     [vc myFunction]; 
    } 
} 
+0

殺手執行 – lol 2012-01-26 04:21:53

3

我發現我是能夠使用UITapGestureRecognizer上一個UISegmentedControl,我認爲應該解決你的問題。

+0

這比我見過的所有建議都要簡單得多。完美地工作。謝謝 :) – 2012-06-16 19:20:19