如何在awakeFromNib之后启用windowWillResize

时间:2013-01-21 15:34:55

标签: objective-c cocoa delegates nswindow

我通过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;
}

1 个答案:

答案 0 :(得分:0)

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

// MyController.h

#import <Foundation/Foundation.h>

@interface MyController : NSViewController <NSWindowDelegate>
-(void) awakeFromNib;
-(NSSize) windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize;
@end