ios UITextView 委托方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11825078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:35:59  来源:igfitidea点击:

UITextView delegate methods

iphoneiosxcodeuitextview

提问by Mrwolfy

I am trying to get delegate methods to work with UITextView, but it's not working for some reason.

我试图让委托方法与 UITextView 一起使用,但由于某种原因它不起作用。

I have declared in my viewController.h that it is a UITextViewDelegate

我在 viewController.h 中声明它是一个 UITextViewDelegate

I am trying to get the following code to work to erase the default code "TEXT" when I tap on the textView.

当我点击 textView 时,我试图让以下代码工作以擦除默认代码“TEXT”。

- (void)textViewDidBeginEditing:(UITextView *)textView {

    if (myTextView.text == @"TEXT") {
        [myTextView setText:@""];
    }

    NSLog(@"did begin editing");
}

I expected to the text to be cleared and to see the NSLog print when I tap on the textView and the keyboard appears. Nothing at all happens

当我点击 textView 并出现键盘时,我希望文本被清除并看到 NSLog 打印。什么都没有发生



Using a text view by the way because I need to scale the view based on its content size and seems that the textView has a contentSize property, whit label and textField do not.

顺便说一下,使用文本视图是因为我需要根据其内容大小缩放视图,并且似乎 textView 具有 contentSize 属性,而 whit label 和 textField 没有。

UPDATE:

更新:

I should have used:

我应该使用:

if ([myTextView.text isEqualToString:@"TEXT"]) {
    [myTextView setText:@""]; }

here is the project if you want to take a look.

如果你想看一看,这里是这个项目。

采纳答案by holex

this method is missing from your Test2ViewController.mfile:

您的Test2ViewController.m文件中缺少此方法:

- (void)viewDidLoad {
    [myTextView setDelegate:self];
}

or you can connect the delegate in the Interface Builderas well, if you prefer that way better.

或者您也可以在Interface Builder 中连接委托,如果您更喜欢这种方式。

UPDATE #1:

更新#1:

add this method to you class for controlling the returnkey.

将此方法添加到您的类中以控制return密钥。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"Return pressed, do whatever you like here");
        return NO; // or true, whetever you's like
    }

    return YES;
}

回答by LJ Wilson

Connect the delegate of the TextView in Interface Builder to the parent class. I like to use the connection in IB rather than code it. To me the less code to look at the better :). Also - don't compare strings that way. Use isEqualToString for string comparison:

将 Interface Builder 中 TextView 的委托连接到父类。我喜欢在 IB 中使用连接而不是编码它。对我来说,代码越少越好:)。另外 - 不要那样比较字符串。使用 isEqualToString 进行字符串比较:

if ([myTextView.text isEqualToString:@"TEXT"]) {
        [myTextView setText:@""];
}

Connection Pic

连接图

Hereis the fixed project:

是固定项目:

回答by andy33gmail

I think I have a good solution in principle:

我想我原则上有一个很好的解决方案:

Set the UITextView's delegate to self and then make your own delegate - re-using the same name. This allows you to intercept the delegate without anything looking different from the outside

将 UITextView 的委托设置为 self,然后创建自己的委托 - 重新使用相同的名称。这使您可以在没有任何与外部不同的情况下拦截委托

@interface TTGTextView : UITextView<UITextViewDelegate>// UIPlaceHolderTextView
@property(nonatomic, assign) id<UITextViewDelegate> delegate;
@synthesize delegate = realDelegate;

Then go ahead and intercept the methods. Note that you need to cover all of them, otherwise they won't react the "real" delegate

然后继续拦截这些方法。请注意,您需要涵盖所有这些,否则他们不会对“真正的”委托做出反应

-(void)textViewDidBeginEditing:(UITextView *)textView
{
  if ([realDelegate respondsToSelector:@selector(textViewDidBeginEditing:)])
    [realDelegate textViewDidBeginEditing:textView];
  /*YOUR CODE HERE*/
}
-(void)textViewDidChange:(UITextView *)textView
{
  if ([realDelegate respondsToSelector:@selector(textViewDidChange:)])
    [realDelegate textViewDidChange:textView];
  /*YOUR CODE HERE*/
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
  if ([realDelegate respondsToSelector:@selector(textViewDidChangeSelection:)])
    [realDelegate textViewDidChangeSelection:textView];
  /*YOUR CODE HERE*/
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
  if ([realDelegate respondsToSelector:@selector(textViewDidEndEditing:)])
    [realDelegate textViewDidEndEditing:textView];
  /*YOUR CODE HERE*/
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  if ([realDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
    return [realDelegate textView:self shouldChangeTextInRange:range replacementText:text];
  return YES;
}
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
  if ([realDelegate respondsToSelector:@selector(textView:shouldInteractWithTextAttachment:inRange:)])
    return [realDelegate textView:self shouldInteractWithURL:URL inRange:characterRange];
  return YES;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)range
{
  if ([realDelegate respondsToSelector:@selector(textView:shouldInteractWithTextAttachment:inRange:)])
    return [realDelegate textView:textView shouldInteractWithTextAttachment:textAttachment inRange:range];
  return YES;
}

Some of the methods fire correctly, others don't fire for me at all but immediately appear for the "real" delegate. But this is a starting point. I guess a more solid and general way would be to make some kind of multiplexer - make it a UITableViewDelegate which holds an array of delegates to fire to.

一些方法正确触发,其他方法根本不会为我触发,但会立即为“真实”委托出现。但这是一个起点。我想更可靠和更通用的方法是制作某种多路复用器 - 使其成为 UITableViewDelegate ,其中包含要触发的委托数组。

回答by Emre Gürses

Deponds on your View. if you use TextField, you should be used UITextFieldDelegate in .header file as a delegate just like show below.

取决于你的观点。如果您使用 TextField,您应该在 .header 文件中使用 UITextFieldDelegate 作为委托,如下所示。

@interface ExamPageViewController : UIViewController <UITextFieldDelegate>

and than we can be used UITextFieldDelegate methods. You can see;

并且我们可以使用 UITextFieldDelegate 方法。你可以看到;

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.questionAnswerTextField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {

    if ([textField.text isEqualToString:@"Sorunun cevab?n? buraya yaz?n?z!"]) {
        textField.text = @"";
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text isEqualToString:@""]) {
        textField.text = @"Sorunun cevab?n? buraya yaz?n?z!";
    }
}

Otherwise you should be used UITextViewDelegate as a delegate like below code snippet.

否则你应该像下面的代码片段一样使用 UITextViewDelegate 作为委托。

@interface ExamPageViewController : UIViewController <UITextViewDelegate>

and than we can be used these delegate methods in .m file

然后我们可以在 .m 文件中使用这些委托方法

- (BOOL)textViewShouldReturn:(UITextView *)textField {
    [self.questionAnswerTextField resignFirstResponder];
    return YES;
}

-(void)textViewDidBeginEditing:(UITextView *)textField {

    if ([textField.text isEqualToString:@"Sorunun cevab?n? buraya yaz?n?z!"]) {
        textField.text = @"";
    }
}


- (void)textViewDidEndEditing:(UITextView *)textField {
    if ([textField.text isEqualToString:@""]) {
        textField.text = @"Sorunun cevab?n? buraya yaz?n?z!";
    }
}

回答by Lithu T.V

Your UITextview needs to be told where its delegate methods lies...

你的 UITextview 需要被告知它的委托方法在哪里......

If you add it via interface builder then simply connect the delegates

如果您通过界面构建​​器添加它,则只需连接代表

or if via code

或者如果通过代码

[yourTextViewOutlet setDelegate:self];

回答by MALIKK HABIB UR REHMAN

Swift 4,5

斯威夫特 4,5

write this extension at the bottom of your controller. it can be use to show and hide placeholder in textView.

在控制器底部写下这个扩展。它可用于在 textView 中显示和隐藏占位符。

extension ViewController: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView == yourTxtView {
            txtNote.text = ""
            txtNote.textColor = .black
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if textView == yourTxtView {
            txtNote.text = "Note:"
            txtNote.textColor = .gray
        }
    }
}