【问题标题】:ShouldChangeTextInRange is not called for UITextView不为 UITextView 调用 ShouldChangeTextInRange
【发布时间】:2017-05-22 11:53:29
【问题描述】:

我使用自定义 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;
                    }
                }
        }

【问题讨论】:

  • 你在 viewDidLoad 中设置了 textviewDelegate self 吗??
  • 我尝试在 Initialize 方法中添加 (delegate=this),但抛出了 invocationtargetexception
  • 添加你的viewdidload:textViewObject.delegate = self
  • 仍然异常:System.InvalidOperationException:事件注册正在覆盖现有委托。要么只使用事件,要么使用你自己的委托
  • 在你的控制器中添加这个 ShouldChangeTextInRange 委托方法而不是 MytextView 类,并使 textViewDelegate 符合你的控制器。

标签: ios xamarin xamarin.ios uitextview


【解决方案1】:

在您的 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 风格的 Delegates 和 C# 匿名 Delegates,否则会报错:

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

【讨论】:

  • 我试过了,但抛出了以下异常: System.Reflection.TargetInvocationException: 调用目标抛出了异常。 ---> System.InvalidOperationException:事件注册正在覆盖现有委托。要么只使用事件,要么使用你自己的委托
  • @Nininea 你为这个UItextView分配了哪些基于C#的事件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
相关资源
最近更新 更多