2

我希望创建一个没有 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(如您在代码中所见),但这无济于事。

非常感谢任何解决此问题的帮助。

4

1 回答 1

1

解决方案是:[self.consoleWindow setReleasedWhenClosed:NO];

以下是完整代码的最终结果(仅相关部分):.h:

@property (strong,nonatomic) NSWindow *consoleWindow;
@property (strong,nonatomic) NSTextView* textView;
@property (strong,nonatomic) NSScrollView* scrollView;

米:

-(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");
     }
于 2013-10-04T08:42:11.883 回答