不为 UITextView 调用 ShouldChangeTextInRange [英] ShouldChangeTextInRange is not called for UITextView

查看:14
本文介绍了不为 UITextView 调用 ShouldChangeTextInRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义 UITextView 并且需要在返回点击时隐藏键盘.我需要捕捉 'ShouldChangeTextInRange' 有 textview 的内容,但我不知道为什么方法没有被调用.这是我的文本视图的代码:

I user custom UITextView and need to hide keyboard on return click. I need to catch 'ShouldChangeTextInRange' what has textview, I don't know why but method is not called. here is code of for my text view :

     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 ("
")) {
                        this.EndEditing (true);
                        return false;
                    } else {
                        return true;
                    }
                }
        }

推荐答案

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

In your UITextView subclass, add IUITextViewDelegate and implement 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("
"))
        {
            this.EndEditing(true);
            return false;
        }
        return true;
    }
}

注意:不能混用ObjC/Swift风格的委托和C#匿名委托,否则会报错:

Note: You can not mix using ObjC/Swift style Delegates and C# anonymous Delegates, otherwise you end up with the error:

事件注册正在覆盖现有委托.要么使用事件,要么使用你自己的委托

Event registration is overwriting existing delegate. Either just use events or your own delegate

这篇关于不为 UITextView 调用 ShouldChangeTextInRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆