【发布时间】: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