2017-10-10 55 views
1

我需要使用兩個單選按鈕來工作,一次只能單擊一個按鈕。使用用戶Alanc Liu在這裏發佈的答案:Radio button in xamarin.ios我已經讓我的視圖控制器看起來正確了,但我無法弄清楚如何監聽水龍頭以將另一個單選按鈕設置爲false。我已經嘗試在ViewDidLoad方法中添加一個手勢識別器,但還沒有得到任何工作(我大多隻是使用以前的故事板來添加方法來按鈕點擊)。Xamarin.iOS中的單選按鈕偵聽水龍頭?

我的視圖控制器:

public partial class VerifyViewController : UIViewController 
{ 
    public VerifyViewController (IntPtr handle) : base (handle) 
    {    
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     MyRadioButton tBtn = new MyRadioButton(new CGPoint(100, 300), "TEXT PHONE"); 
     MyRadioButton eBtn = new MyRadioButton(new CGPoint(100, 375), "EMAIL"); 

     this.Add(tBtn); 
     this.Add(eBtn); 
    } 
} 

他的單選按鈕類:

public class MyRadioButton : UIView 
{ 
private CircleView circleView; 
private UILabel lbTitle; 

public bool State { 
    get { 
     return circleView.State; 
    } 
    set { 
     circleView.State = value; 
    } 
} 

public MyRadioButton (CGPoint pt,string title) 
{ 
    this.Frame = new CGRect (pt, new CGSize (150, 30)); 
    circleView = new CircleView (new CGRect(0, 0, 30, 30)); 
    lbTitle = new UILabel (new CGRect (30, 0, 120, 30)); 
    lbTitle.Text = title; 
    lbTitle.TextAlignment = UITextAlignment.Center; 
    this.AddSubview (circleView); 
    this.AddSubview (lbTitle); 
    this.BackgroundColor = UIColor.FromRGBA(1,0,0,0.3f); 

    UITapGestureRecognizer tapGR = new UITapGestureRecognizer (() => { 
     State = !State; 
    }); 
    this.AddGestureRecognizer (tapGR); 
} 
} 

class CircleView : UIView 
{ 
private bool state = false; 
public bool State { 
    get { 
     return state; 
    } 
    set { 
     state = value; 
     this.SetNeedsDisplay(); 
    } 
} 

public CircleView (CGRect frame) 
{ 
    this.BackgroundColor = UIColor.Clear; 
    this.Frame = frame; 
} 

public override void Draw (CoreGraphics.CGRect rect) 
{ 
    CGContext con = UIGraphics.GetCurrentContext(); 

    float padding = 5; 
    con.AddEllipseInRect (new CGRect (padding, padding, rect.Width - 2 * padding, rect.Height - 2 * padding)); 
    con.StrokePath(); 

    if (state) { 
     float insidePadding = 8; 
     con.AddEllipseInRect (new CGRect (insidePadding, insidePadding, rect.Width - 2 * insidePadding, rect.Height - 2 * insidePadding)); 
     con.FillPath(); 
    } 
} 
} 

回答

1
  1. 在MyRadioButton公開公共事件,調用它,當我們挖掘的單選按鈕。

    守則MyRadioButton

    //define the event inside MyRadioButton 
    public delegate void TapHandler(MyRadioButton sender); 
    public event TapHandler Tap; 
    
    //call it in MyRadioButton(CGPoint pt, string title) 
    UITapGestureRecognizer tapGR = new UITapGestureRecognizer(() => { 
        State = !State; 
        Tap(this); 
    }); 
    
  2. 處理該事件的的viewController

    代碼裏面ViewController

    MyRadioButton tBtn = new MyRadioButton(new CGPoint(100, 300), "TEXT PHONE"); 
    MyRadioButton eBtn = new MyRadioButton(new CGPoint(100, 375), "EMAIL"); 
    
    this.Add(tBtn); 
    this.Add(eBtn); 
    
    tBtn.Tap += Btn_Tap; 
    eBtn.Tap += Btn_Tap; 
    // set the default selection 
    Btn_Tap(tBtn); 
    
    MyRadioButton PreviousButton; 
    private void Btn_Tap(MyRadioButton sender) 
    { 
        if(PreviousButton != null) 
        { 
         //set previous to false 
         PreviousButton.State = false; 
        } 
        //set current to true 
        sender.State = true; 
        //assign current to previous 
        PreviousButton = sender; 
    } 
    

結果:

enter image description here