从textview在控制台中打印文本

时间:2015-11-09 20:04:10

标签: ios objective-c textview

我的项目中有一个文本视图,当用户执行某些操作时会更改不同的文本。但是我如何在控制台中打印文本。因此,每次文本更改时,它也会在控制台中显示。

这是我的文本视图代码。

self.heardTextView.text = [NSString stringWithFormat:@"Heard: \"%@\"", hypothesis]; // Show it in the status box.

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式实现这一目标:

一种方法是简单地使用NSLog(@" the text is %@", self.heardTextView.text)语句

您可以使用apple提供的本机委托方法:

实施此操作以在对textview进行任何更改后打印消息

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

        NSLog(@" the text is %@", self.heardTextView.text)

        if([self.heardTextView.text isEqualToString:@"hello"]){
         //do something
        }
  }

只要textView文本中有chnage,您也可以使用KVO进行观察。

heardTextView.text=@"old";
[heardTextView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];
heardTextView.text=@"hello";

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    NSLog(@"the text changed");
    NSLog(@"changed text is %@",change[@"new"]);
}
相关问题