【问题标题】:Closing NSWindow programmatically以编程方式关闭 NSWindow
【发布时间】:2012-08-11 19:49:20
【问题描述】:

我正在尝试以编程方式打开和关闭一个窗口。这是我的尝试,我打开但没有关闭它。 我已经尝试了很多方法,但我认为我必须对 NSWindow 做一些事情才能让它在其他部分工作。

AppDelegate.h

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import <AVFoundation/AVFoundation.h>

@class AVPlayer;
@interface AppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet WebView *news;
    IBOutlet NSButton *play;
    IBOutlet NSButton *options;
    IBOutlet NSButton *realmlist;
    IBOutlet NSTextField *version;
    AVPlayer *audioPlayer;
}

@property (assign) IBOutlet NSWindow *window;

@property (nonatomic, retain) IBOutlet WebView *news;
@property (nonatomic, retain) IBOutlet NSTextField *version;
@property (nonatomic, retain) AVPlayer *audioPlayer;

-(IBAction)play:(id)sender;
-(IBAction)changeRealmlist:(id)sender;
-(IBAction)close:(id)sender;
-(IBAction)removeCache:(id)sender;
-(IBAction)sOptions:(id)sender;
@end

AppDelegate.m

-(IBAction)sOptions:(id)sender{
    NSRect frame = NSMakeRect(670, 400, 451, 351);
    NSUInteger styleMask =    NSBorderlessWindowMask;
    NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
    NSWindow * optionsWindow =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
    [optionsWindow setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"opcionesbackground.gif"]]];
    [optionsWindow makeKeyAndOrderFront: optionsWindow];
    [optionsWindow setMovable:YES];

    NSRect deleteCache = NSMakeRect(225, 200, 89, 36);

    NSButton *button = [[NSButton alloc] initWithFrame:deleteCache];
    [button setButtonType:NSMomentaryPushInButton];
    //[button setImage:[NSImage imageNamed:@"rest_acepton.gif"]];
    [button setTitle:@"Eliminar cache"];
    [[button cell] setBackgroundColor:[NSColor redColor]];
    [button setAction:@selector(removeCache:)];
    [[optionsWindow contentView] addSubview:button];
    [button setNeedsDisplay:YES];

    NSRect accept = NSMakeRect(350, 20, 89, 36);

    NSButton *aceptar = [[NSButton alloc] initWithFrame:accept];
    [aceptar setButtonType:NSMomentaryPushInButton];
    [aceptar setImage:[NSImage imageNamed:@"rest_acepton.gif"]];
    [aceptar setBordered:NO];
    [aceptar setTarget:optionsWindow];
    [aceptar setAction:@selector(closeOptions:)];
    [[optionsWindow contentView] addSubview:aceptar];
    [aceptar setNeedsDisplay:YES];
}

-(void)closeOptions:(id)sender{
    [optionsWindow orderOut:nil];
}

【问题讨论】:

  • 嗯.. 你为什么不再使用[self.window close];
  • 调用 performClose 怎么样
  • Fixed :D 线索在这里:NSUInteger styleMask = NSClosableWindowMask;
  • 您应该发布您的答案,以便将问题标记为已解决。

标签: objective-c macos cocoa nswindow


【解决方案1】:

根据您的来源,'optionsWindow' 是方法 sOptions 的 ivar。你怎么能在另一种方法中使用?能否请您发布正确的来源

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 2015-03-17
    • 2012-09-08
    • 2013-08-08
    • 2015-03-17
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多