不为UITextView调用ShouldChangeTextInRange

时间:2017-05-22 11:53:29

标签: ios xamarin xamarin.ios uitextview

我用户自定义UITextView并需要在返回点击时隐藏键盘。我需要抓住' ShouldChangeTextInRange'什么有textview,我不知道为什么但是  方法未被调用。这是我的文本视图的代码:

     public class PlaceholderTextView : UITextView
        { 
public PlaceholderTextView ()
        {
            Initialize ();
        }

        public PlaceholderTextView (CGRect frame)
            : base (frame)
        {
            Initialize ();
        }

        public PlaceholderTextView (IntPtr handle)
            : base (handle)
        {
            Initialize ();
        }

        void Initialize ()
        {
            Text = Placeholder;

            ShouldBeginEditing = t => {
                if (Text == Placeholder)
                    Text = string.Empty;

                return true;
            };

            ShouldEndEditing = t => {
                if (string.IsNullOrEmpty (Text))
                    Text = Placeholder;
                return true;
            };

        }
        public override bool ShouldChangeTextInRange (UITextRange inRange, string replacementText)
                {
                    if (Text.Equals ("\n")) {
                        this.EndEditing (true);
                        return false;
                    } else {
                        return true;
                    }
                }
        }

1 个答案:

答案 0 :(得分:3)

在您的UITextView子类中,添加IUITextViewDelegate并实施ShouldChangeText(selector = textView:shouldChangeTextInRange:replacementText:):

public class MyTextView : UITextView, IUITextViewDelegate
{
    string Placeholder;

    public MyTextView()
    {
        Initialize();
    }

    public MyTextView(Foundation.NSCoder coder) : base(coder)
    {
        Initialize();
    }

    public MyTextView(Foundation.NSObjectFlag t) : base(t)
    {
        Initialize();
    }

    public MyTextView(IntPtr handle) : base(handle)
    {
        Initialize();
    }

    public MyTextView(CoreGraphics.CGRect frame) : base(frame)
    {
        Initialize();
    }

    public MyTextView(CoreGraphics.CGRect frame, NSTextContainer textContainer) : base(frame, textContainer)
    {
        Initialize();
    }

    void Initialize()
    {
        Delegate = this;
    }

    [Export("textViewShouldBeginEditing:")]
    public bool ShouldBeginEditing(UITextView textView)
    {
        if (Text == Placeholder)
            Text = string.Empty;

        return true;
    }

    [Export("textViewShouldEndEditing:")]
    public bool ShouldEndEditing(UITextView textView)
    {
        if (string.IsNullOrEmpty(Text))
            Text = Placeholder;
        return true;
    }

    [Export("textView:shouldChangeTextInRange:replacementText:")]
    public bool ShouldChangeText(UITextView textView, NSRange range, string text)
    {
        if (text.Contains("\n"))
        {
            this.EndEditing(true);
            return false;
        }
        return true;
    }
}

注意:您不能使用ObjC / Swift样式代理和C#匿名代理混合,否则您最终会得到错误:

  

事件注册会覆盖现有代理。要么只使用事件,要么使用自己的代理