我用戶自定義的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;
}
}
}
已經設置在viewDidLoad中textviewDelegate自我? –
我試圖在Initialize方法中添加(delegate = this),但是拋出invocationtargetexception – Nininea
在你的viewdidload中添加:textViewObject.delegate = self –