如何在进入 UITextView(用户点击以编辑它)和离开视图(用户点击以离开它)时调用一些代码?
感谢任何帮助。
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextView委托协议/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
在这里您可以找到几种有用的调查方法:
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
斯威夫特示例
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")
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)