0

我通过 awakeFromNib 初始化窗口,我希望当我调整窗口大小时,它的宽度和高度会加倍。代码一开始运行正确,但第二次它以两倍大小出现,所以我想知道如何禁止 windowWillResize 直到我调整它的大小,它似乎 awakeFromNib 不起作用。这是代码:

-(void)awakeFromNib{
    width = 388.0;
    height = 273.0;
    NSRect rect = window.frame;
    NSSize size;
    size.width = width;
    size.height = height;
    rect.size = size;
    [window setFrame:rect display:YES];    
    [window setDelegate:self];
}
-(NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize{
    float rwidth = [window frame].size.width;
    float rheight = [window frame].size.height;
    if(rwidth<=width*2 && rheight<=height*2){
        NSRect rect = window.frame;
        NSSize size;
        size.width = width*2;
        size.height = height*2;
        rect.size = size;
        [window setFrame:rect display:YES];
        [window setMinSize:[window frame].size];
        [window setMaxSize:[window frame].size];
    }
    return frameSize;
}
4

1 回答 1

0

你的控制器应该在你的头文件中实现 NSWindowDelegate 协议

// MyController.h

#import <Foundation/Foundation.h>

@interface MyController : NSViewController <NSWindowDelegate>
-(void) awakeFromNib;
-(NSSize) windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize;
@end
于 2013-07-18T16:52:52.777 回答