2011-11-21 30 views
3

我在嘗試使用UISegmentedControl來控制我的表的排序順序,特別是我想在已經選擇的段上註冊一個tap(以反轉我的排序) 。因此,我試圖在任何細分被點擊時提出UIControlEvent.ValueChanged事件。MonoTouch:UISegmentedControl選擇一個已經選擇的段

我已覆蓋從UISegmentedControlSelectedSegment和我使用我在Xcode元素這個自定義類,但是,它看起來好像當該段控制被點擊這個代碼不叫(這當視圖稱爲加載雖然= S)。

​​

這個問題已經在之前問:UISegmentedControl register taps on selected segment,但是這個問題是ObjC這個問題是針對C#/ MonoTouch的。

回答

2

您提供的問題link的最後一個答案包含最好的(工作良好的)答案(我贊成它,你也應該;-)。

注意:從評論看來,以前的答案是(可能)在iOS4上工作,但不在iOS5上。

以下是您要查找的代碼。我使用RectangleF .ctor來測試它,如果你使用其他的東西(或者它不像你原來的代碼那麼複雜,例如沒有選擇器來繼承它),你可能需要添加你自己的.ctor。

class MyUISegmentedControl : UISegmentedControl { 

     public MyUISegmentedControl (RectangleF r) : base (r) {} 

     public override void TouchesBegan (NSSet touches, UIEvent evt) 
     { 
      int current = SelectedSegment; 
      base.TouchesBegan (touches, evt); 
      if (current == SelectedSegment) 
       SendActionForControlEvents (UIControlEvent.ValueChanged); 
     } 
    } 
+0

感謝poupou。對不起,我花了這麼長時間才把這個標記爲答案,我不願意做不同的事情。你的迴應非常有幫助! – Symmetry

+0

注意自我...「問題鏈接的最後一個答案」是模棱兩可的,是一個移動的目標。最好提供一個答案的鏈接,而不是問題。 –

0

提供的鏈接對iOS7有很好的答案,但是使用Objective C/Swift,特別是當再次按下當前選定的段時取消選擇段。

這裏是需要使用TouchesEnded事件而不是TouchesBegan的C#版本。使用TouchesBegan爲我引發了兩次UISegmentedControl.ValueChanged事件,這導致了不正確的行爲。

此代碼包括處理段被觸摸並拖動控件取消事件。

class OptionalSegmentedControl : UISegmentedControl 
{ 


    public override void TouchesEnded(NSSet touches, UIEvent evt) 
    { 

     // Getting touch information to work out whether segment has been pressed, then dragged off to cancel the event 
     // Comment these lines out if you don't care about handling this 
     CGPoint locationPoint = ((UITouch)touches.AnyObject).LocationInView(this); 
     CGPoint viewPoint = this.ConvertPointFromView(locationPoint, this); 

     // Save the selected segment first 
     nint current = this.SelectedSegment; 

     // then fire the event 
     base.TouchesEnded(touches, evt); 


     // Check if the touch point is still on the control when the touch has ended, 
     // as well as comparing the current and new selected segment values 
     // and then fire the ValueChanged event if the same segment is pressed 


     // Use this line and comment the second if statement if you don't care about handling click and drag to cancel the event 
     //if (current == this.SelectedSegment) 
     if ((this.PointInside(viewPoint, evt)) && (current == this.SelectedSegment)) 
     { 
      this.SelectedSegment = -1; 
      SendActionForControlEvents(UIControlEvent.ValueChanged); 
     } 
    } 


} 

然後在ValueChanged事件,你做你需要做的UISegmentedControl什麼:

  // cell.SegmentedControl for me was a UISegmented control in a UITableViewCell 
      cell.SegmentedControl.ValueChanged += (s, e) => 
      { 
       OptionalSegmentedControl osc = (OptionalSegmentedControl)s; 

       // continue with your code logic..... 

      }; 

有一點要注意的是,我開始用的touchesBegan無效,但後來更名爲當我意識到這是iOS7及更高版本所需要的東西時觸摸已結束。仍然沒有工作,直到我意識到我必須也改變了線路:

base.TouchesEnded(touches, evt); 

所以不要忘記改變,如果你的距離的touchesBegan轉換爲TouchesEnded。

相關問題