【问题标题】:Detect Start and Stop Editing UITextView检测开始和停止编辑 UITextView
【发布时间】:2011-05-07 03:34:51
【问题描述】:

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

感谢任何帮助。

【问题讨论】:

    标签: iphone objective-c uitextview editing


    【解决方案1】:

    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
    

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

    【讨论】:

    • @EnginYapici 确定,添加
    【解决方案2】:

    您还可以实现 UITextViewDidChange 委托方法。我在我的应用程序中使用下面的代码来快速记笔记。每当用户输入一个字符时,观察者就会从通知中心捕捉到它并调用 saveText 方法。

    方法如下:

    将此行添加到 viewDidLoad 方法:

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    

    这些行位于代码中的适当位置(例如处理文本视图委托方法的部分。提示:为此使用编译指示标记(#pragma mark - TextView 委托方法):

    - (void)textViewDidChange:(UITextView *)textView{
    
        NSLog(@"textViewShouldEndEditing"); // Detect in log output if the method gets called
        [self saveText:nil]; // Call any method you like
    
    }
    

    【讨论】:

    • 方法签名应该是- (void)textViewDidChange:(NSNotification *)notification。来自documentation:“受影响的视图存储在通知的object参数中。”
    【解决方案3】:

    使用委托并使用:

    - (void) textViewDidBeginEditing:(UITextView *) textView {
        // Your code here
    }
    

    【讨论】:

      【解决方案4】:
      txtviewaddress.text="Address"
      
      txtViewAddress.TextColor = UIColor.LightGray;
      txtViewAddress.ShouldBeginEditing += (textView) =>
              {
       txtViewAddress.Text = "";
                  txtViewAddress.TextColor = UIColor.Black;
                  return true;
      };
      txtViewAddress.ShouldEndEditing += (textView) =>
              {
                  if (textView.Text == "")
                  {
                      txtViewAddress.Text = " Address";
                      txtViewAddress.TextColor = UIColor.LightGray;
      
                  }
                  return true;
              };
      

      【讨论】:

      • 嗨!谢谢回答。但是,您能否添加更多详细信息?仅代码答案通常不是很有效。
      • 用于在TextView [Xamarin IOS]中设置占位符文本
      猜你喜欢
      • 1970-01-01
      • 2014-08-21
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2014-02-16
      • 2018-06-20
      相关资源
      最近更新 更多