我希望创建一个没有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,但这无济于事。
非常感谢您对解决此问题的任何帮助。
解决方案是: [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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句