Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.iOS中的单选按钮是否收听点击?_Xamarin.ios - Fatal编程技术网

Xamarin.iOS中的单选按钮是否收听点击?

Xamarin.iOS中的单选按钮是否收听点击?,xamarin.ios,Xamarin.ios,我需要得到一个有两个单选按钮的视图,一次只能点击一个。使用用户Alanc Liu在这里发布的答案:我的视图控制器看起来是正确的,但我不知道如何监听点击以将另一个单选按钮设置为false 我尝试过在ViewDidLoad方法中添加一个手势识别器,但还没有得到任何效果(我以前主要是使用故事板为按钮点击添加方法) 我的视图控制器: public partial class VerifyViewController : UIViewController { public VerifyViewCo

我需要得到一个有两个单选按钮的视图,一次只能点击一个。使用用户Alanc Liu在这里发布的答案:我的视图控制器看起来是正确的,但我不知道如何监听点击以将另一个单选按钮设置为false

我尝试过在ViewDidLoad方法中添加一个手势识别器,但还没有得到任何效果(我以前主要是使用故事板为按钮点击添加方法)

我的视图控制器:

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 ();
    }
}
}
  • 在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);
    });
    
  • 在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;
    }
    
  • 结果: