2017-05-22 62 views
2

我用戶自定義的UITextView,並且需要在返回點擊時隱藏鍵盤。我需要趕上'ShouldChangeTextInRange'有什麼textview,我不知道爲什麼,但方法不被調用。這裏是我的文本視圖代碼:ShouldChangeTextInRange不被調用UITextView

 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; 
        } 
       } 
     } 
+0

已經設置在viewDidLoad中textviewDelegate自我? –

+0

我試圖在Initialize方法中添加(delegate = this),但是拋出invocationtargetexception – Nininea

+0

在你的viewdidload中添加:textViewObject.delegate = self –

回答

1

在你UITextView子類,添加IUITextViewDelegate和實施ShouldChangeText(選擇= 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 /雨燕風格代表和C#匿名委託,否則你最終錯誤:

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

+0

我試過這個,但拋出以下異常: System.Reflection.TargetInvocationException:調用的目標引發了異常。 ---> System.InvalidOperationException:事件註冊覆蓋現有的委託。要麼只是使用事件或你自己的代理 – Nininea

+0

@Nininea你分配給這個「UItextView」的基於C#的事件是什麼? – SushiHangover

+0

我已更新問題 – Nininea

相關問題