xcode 如何检查 UITextView 何时被编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8141157/
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-09-14 22:26:27  来源:igfitidea点击:

How can I check when a UITextView is being edited?

iphoneobjective-ciosxcodeuitextview

提问by pixelbitlabs

I'm using the following code but it is not being recognised when I start editing the UITextView:

我正在使用以下代码,但在我开始编辑 UITextView 时无法识别它:

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

[done setHidden:NO];

NSLog(@"Started editing target!");

}

Why is this?

为什么是这样?

I have added the delegate to the .h file too.

我也将委托添加到 .h 文件中。

回答by Matteo Alessani

Try to set the delegate of your UITextView. You need to connect the outlet to the delegate in Interface Builder or if you are creating it programmatically you can:

尝试设置 UITextView 的委托。您需要将插座连接到 Interface Builder 中的委托,或者如果您以编程方式创建它,您可以:

yourTextView.delegate = self;

to set the delegate to the controller you are inserting your element.

将委托设置为您要插入元素的控制器。

回答by ohho

if ([self.textView isFirstResponder]) ...

UPDATE: I include the full source to indicate UITextView definitely responds to isFirstResponder @d2burke

更新:我包含完整的源代码以表明 UITextView 肯定响应 isFirstResponder @d2burke

#import "TextViewController.h"

@interface TextViewController ()
@property (nonatomic, retain) IBOutlet UITextView *textView;
@end

@implementation TextViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)click:(UIButton *)check
{
    BOOL isActive = [self.textView isFirstResponder];
    NSLog(@"%d", isActive);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

回答by badeleux

make sure that you:

确保您:

  1. nameOfTextView.delegate = self;
  2. show compiler you are using UITextViewDelegate in your class
  1. nameOfTextView.delegate = self;
  2. 显示您在类中使用 UITextViewDelegate 的编译器

回答by Nagarjun

  1. Firstly you have to set UIViewController <UITextViewDelegate>
  2. Set

    yourTextview.delegate = self;

  3. And catch this delegate using

    -(void)textViewDidBeginEditing:(UITextView *)textView { [self.yourTextview setText:@""]; [yourTextview setTextColor:[UIColor blackColor]]; }

  1. Firstly you have to set UIViewController <UITextViewDelegate>
  2. Set

    yourTextview.delegate = self;

  3. And catch this delegate using

    -(void)textViewDidBeginEditing:(UITextView *)textView { [self.yourTextview setText:@""]; [yourTextview setTextColor:[UIColor blackColor]]; }

回答by Bill Burgess

Also could be a "duh" answer, but if you created your textView in IB, did you remember to connect the outlet up?

也可能是“废话”的答案,但是如果您在 IB 中创建了 textView,您是否记得将插座连接起来?