检测开始和停止编辑UITextView [英] Detect Start and Stop Editing UITextView

查看:114
本文介绍了检测开始和停止编辑UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输入UITextView(用户点按以编辑它)并离开视图(用户点击以离开它)时调用某些代码?

How can I call some code upon entering a UITextView (user taps to edit it) and leaving the view (user taps to leave it)?

感谢任何帮助。

推荐答案

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/参考/ UITextViewDelegate.html#// apple_ref / occ / intf / UITextViewDelegate

在这里你可以找到几个有用的方法来研究:

Here you can find several useful methods to investigate:


  • textViewDidBeginEditing:

  • textViewDidEndEditing:

  • textViewDidBeginEditing:
  • textViewDidEndEditing:

此外要住 UITextView 你经常应该这样做实现调用 [yourTextView resignFirstResponder]的行动;

Moreover to live UITextView you often should implement action that calls [yourTextView resignFirstResponder];

Objective-C示例le

//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")
    }
}

这篇关于检测开始和停止编辑UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆