我希望创建一个没有nib文件的窗口,该文件完全由NSTextView组成。
它应该充当弹出窗口,但仍然是无模式的。
到目前为止,我有两个属性:
@property (strong,nonatomic) NSWindow *consoleWindow;
@property (strong,nonatomic) NSTextView* textView;
以下是我的实现:
-(void)doubleAction:(NSOutlineView*)sender
{
if(self.currentLogEntry == nil)
{
return;
}
self.consoleWindow = nil;
self.textView = nil;
NSRect windowRect = NSMakeRect(10.0f, 10.0f, 500.0f, 400.0f);
self.consoleWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask)
backing:NSBackingStoreBuffered defer:NO];
self.textView = [[NSTextView alloc] initWithFrame:[[self.consoleWindow contentView] frame]];
[self.textView setString:self.currentLogEntry.value];
[self.consoleWindow setContentView:self.textView];
[self.consoleWindow makeKeyAndOrderFront:nil];
[self.consoleWindow makeFirstResponder:self.textView];
NSLog(@"Double clicked");
}
事情已经连线,所以我有一个条目列表,每当我双击一个条目时,所选条目被加载到self.currentLogEntry
,然后这个方法被触发。它有效但如果我关闭窗口并尝试打开另一个条目,我会得到Thread 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
我认为它与窗口没有被正确释放有关但我尝试了一些事情,比如每当程序进入doubleAction时将属性设置为nil(正如你在代码中看到的那样)但是没有帮助
非常感谢任何解决此问题的帮助。
答案 0 :(得分:1)
解决方案是:[self.consoleWindow setReleasedWhenClosed:NO];
以下是完整代码的结果(仅限相关部分): .H:
@property (strong,nonatomic) NSWindow *consoleWindow;
@property (strong,nonatomic) NSTextView* textView;
@property (strong,nonatomic) NSScrollView* scrollView;
的.m:
-(void)doubleAction:(NSOutlineView*)sender
{
if(self.currentLogEntry == nil)
{
return;
}
self.consoleWindow = nil;
self.textView = nil;
self.scrollView = nil;
NSRect windowRect = NSMakeRect(10.0f, 10.0f, 500.0f, 400.0f);
self.consoleWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask)
backing:NSBackingStoreBuffered defer:NO];
[self.consoleWindow setReleasedWhenClosed:NO];
self.scrollView = [[NSScrollView alloc] initWithFrame:[[self.consoleWindow contentView] frame]];
[self.scrollView setBorderType:NSNoBorder];
[self.scrollView setHasVerticalScroller:YES];
[self.scrollView setHasHorizontalScroller:NO];
[self.scrollView setAutoresizingMask:NSViewWidthSizable |
NSViewHeightSizable];
NSSize contentSize = [self.scrollView contentSize];
self.textView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0,
contentSize.width, contentSize.height)];
[self.textView setMinSize:NSMakeSize(0.0, contentSize.height)];
[self.textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[self.textView setVerticallyResizable:YES];
[self.textView setHorizontallyResizable:NO];
[self.textView setAutoresizingMask:NSViewWidthSizable];
[[self.textView textContainer]
setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)];
[[self.textView textContainer] setWidthTracksTextView:YES];
[self.textView setString:self.currentLogEntry.value];
[self.scrollView setDocumentView:self.textView];
[self.consoleWindow setContentView:self.scrollView];
[self.consoleWindow makeKeyAndOrderFront:nil];
[self.consoleWindow makeFirstResponder:self.textView];
NSLog(@"Double clicked");
}