【问题标题】:Displaying text in text view from NSArray在 NSArray 的文本视图中显示文本
【发布时间】:2015-05-12 12:57:41
【问题描述】:

此方法在AppDelagate 中找到,加载我选择的文本文件并将文本文件的上下文拆分为一个数组。

我无法在我的NSScrollview * 中显示名为self.textView 的数组内容。

我不确定如何使用数组的每个成员更新文本视图。

- (IBAction)loadButton:(id)sender {        
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    if ([panel runModal] == NSFileHandlingPanelOKButton) {
        NSURL *bookUrl  = [panel URL];
        NSString *contents = [NSString stringWithContentsOfURL: bookUrl encoding: NSASCIIStringEncoding error: NULL];
        NSArray *loadedBook = [contents componentsSeparatedByString:@"#NP#"];
        self.textView.value = loadedBook[0];            
    }
}

【问题讨论】:

  • 我不知道textView.value 属性是什么。不应该是textView.text吗?
  • textView.text 不是我可以分配的属性。价值是我认为可以接近我需要的唯一价值。
  • 谷歌搜索“NSTextView 如何更改文本”很难吗?这是第一个结果:stackoverflow.com/questions/14646163/…
  • 文本视图在appdelegate.h文件中声明为属性@property (weak) IBOutlet NSScrollView *textView;
  • 你到底为什么要把一个 NSScrollView 对象命名为textView

标签: objective-c macos nstextview


【解决方案1】:

正确的方法是setString:,在NSText中声明。

你的代码应该是:

- (IBAction)loadButton:(id)sender {        
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    if ([panel runModal] == NSFileHandlingPanelOKButton) {
        NSURL *bookUrl  = [panel URL];
        NSString *contents = [NSString stringWithContentsOfURL: bookUrl encoding: NSASCIIStringEncoding error: NULL];
        NSArray *loadedBooks = [contents componentsSeparatedByString:@"#NP#"];
        [self.textView setString:bookStr];
    }
}

更新

看看at this question,看看如何向 NSScrollView 添加文本

【讨论】:

    猜你喜欢
    • 2019-11-28
    • 2016-11-25
    • 2019-03-13
    • 1970-01-01
    • 2014-02-06
    • 2021-10-08
    • 1970-01-01
    • 2014-03-17
    • 2021-10-24
    相关资源
    最近更新 更多