进入UITextView(用户点击以对其进行编辑)并离开 View (用户点击以使其离开)时,如何调用某些代码?
感谢任何帮助。
最佳答案
textViewDidBeginEditing:
textViewDidEndEditing:
此外,要直播
UITextView
,您通常应该执行调用[yourTextView resignFirstResponder];
的操作Objective-C示例
//you may specify UITextViewDelegate protocol in .h file interface, but it's better not to expose it if not necessary
@interface ExampleViewController()<UITextViewDelegate>
@end
@implementation ExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
//assuming _textView is already instantiated and added to its superview
_textView.delegate = self;
}
//it's nice to separate delegate methods with pragmas but it's up to your local code style policy
#pragma mark UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
//handle user taps text view to type text
}
- (void)textViewDidEndEditing:(UITextView *)textView {
//handle text editing finished
}
@end
Swift示例
class TextViewEventsViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var exampleTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.exampleTextView.delegate = self
}
func textViewDidBeginEditing(_ textView: UITextView) {
print("exampleTextView: BEGIN EDIT")
}
func textViewDidEndEditing(_ textView: UITextView) {
print("exampleTextView: END EDIT")
}
}