我要註冊一個UISegmentedControl
功能的事件UIControlEventTouchUpInside
- 不UIControlEventValueChanged
。我特別想確保只有在用戶觸及項目時觸發,而不是以編程方式設置其值。如何在UISegementedControl上註冊UIControlEventTouchUpInside?
有什麼建議嗎?
我要註冊一個UISegmentedControl
功能的事件UIControlEventTouchUpInside
- 不UIControlEventValueChanged
。我特別想確保只有在用戶觸及項目時觸發,而不是以編程方式設置其值。如何在UISegementedControl上註冊UIControlEventTouchUpInside?
有什麼建議嗎?
我發現的關鍵是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];
}
}
我發現我是能夠使用UITapGestureRecognizer上一個UISegmentedControl,我認爲應該解決你的問題。
這比我見過的所有建議都要簡單得多。完美地工作。謝謝 :) – 2012-06-16 19:20:19
殺手執行 – lol 2012-01-26 04:21:53