Xcode NSTextView';s NSTextStorage正在返回null
我关注developer.apple.com上的OSX教程系列,遇到了一个问题。当我将文本粘贴或键入NSTextView并尝试将其发送到常规粘贴板时,NSTextView返回空字符串。然后,我尝试查看NSTextStorage是否为null,它是否也返回为null。以下是h和m文件的相关部分Xcode NSTextView';s NSTextStorage正在返回null,xcode,macos,nstextview,Xcode,Macos,Nstextview,我关注developer.apple.com上的OSX教程系列,遇到了一个问题。当我将文本粘贴或键入NSTextView并尝试将其发送到常规粘贴板时,NSTextView返回空字符串。然后,我尝试查看NSTextStorage是否为null,它是否也返回为null。以下是h和m文件的相关部分 // Document.m #import "Document.h" @implementation Document @synthesize the_view; //NSTextView @syn
// Document.m
#import "Document.h"
@implementation Document
@synthesize the_view; //NSTextView
@synthesize showStuff; //NSScrollView
- (IBAction)copy2:(id)sender {
NSLog(@"copying");
NSTextStorage *ts = [the_view textStorage];
if( ts )
NSLog(@"not null");
NSString *text = [[the_view textStorage] string];
NSLog(@"%@",text);
//NSString *text = @"okay";
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil];
[pb setString:text forType:NSStringPboardType];
}
//Document.h
#import <Cocoa/Cocoa.h>
@interface Document : NSDocument
{
NSTextView *the_view;
}
@property (nonatomic, retain) IBOutlet NSTextView *the_view;
- (IBAction)copy2:(id)sender;
- (IBAction)paste2:(id)sender;
@property (weak) IBOutlet NSScrollView *showStuff;
@end
//Document.m
#导入“Document.h”
@执行文件
@综合_观点//NSTextView
@综合展示材料//NSScrollView
-(iAction)副本2:(id)发件人{
NSLog(“复制”);
NSTextStorage*ts=[查看文本存储];
如果(ts)
NSLog(@“非空”);
NSString*text=[[u视图文本存储]字符串];
NSLog(@“%@”,文本);
//NSString*text=@“好”;
NSPasteboard*pb=[NSPasteboard generalPasteboard];
[pb declareTypes:[NSArray arrayWithObjects:NSStringPboardType,nil]所有者:nil];
[pb setString:text forType:NSStringPboardType];
}
//文件.h
#进口
@接口文档:NSDocument
{
NSTextView*_视图;
}
@属性(非原子,保留)ibnstextview*_视图;
-(iAction)副本2:(id)发送者;
-(iAction)粘贴2:(id)发送方;
@属性(弱)IBMScrollView*showStuff;
@结束
在上面,您会注意到我有一个字符串“text”的注释版本,它被设置为“ok”。如果我用它,单词“OK”会放在普通的粘贴板上
因此,我的问题是,如果NSTEXT视图中存在有效文本,您是否可以找出NSTextStorage返回null的任何原因。感谢您的帮助。您是否连接了IB中的插座?我对插座的理解很弱。我确实在NSTextView和头文件之间拖动了一个连接。除此之外,在这种情况下,我不知道如何处理插座。为了验证插座连接是否正确,请检查“theu view”是否为零。在得到Uchiugaka的提示后,我去搜索插座的信息,这让我制作了一个新的测试应用程序,其中包含几个相互对话的组件。现在我对它们的工作原理有了更好的了解,这使我得出结论:插座连接不正确是导致原始问题的原因。