【问题标题】:Cocoa: Triggering NSWindow makeKeyAndOrderFront instance method from another classCocoa:从另一个类触发 NSWindow makeKeyAndOrderFront 实例方法
【发布时间】:2017-07-17 09:16:29
【问题描述】:

不是关于窗口的外观 - 或者 - 关于传递参数,但能够调用 makeKeyAndOrderFront通过代码的实例方法(IB 接收器操作工作正常,窗口实现块中的方法也一样),来自 另一个 类。

我知道这应该是微不足道的,而且我没有正确掌握消息结构或正确地做我的声明,但我已经开始绕圈子了,SO 和其他博客和论坛上的许多问题都没有已经足够接近这种确切的时尚来帮助我点击我要去的地方。

虽然我已经尝试了很多事情,但这(虽然不正确)可能是最明显的尝试来展示我尝试要做的事情:

NSWindow 子类

@interface hTaskWindow
- (id)initWithContentRect:// etc...

- (IBAction)hWindowActivate:(id)sender;

// ...
@end 
@implementation hTaskWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {

    self = [super initWithContentRect:contentRect styleMask:NSFullSizeContentViewWindowMask backing:NSBackingStoreBuffered defer:NO];

    [self setTitle:titlename];
    [self setStyleMask:[self styleMask] | //


// 
// Plus a bunch  
// of style
// customisations here
//
// "Visible at launch" is Unticked on this Window.
// 
//

-(void)activateDebugWindow {   //       <---------  I just want to call this from anywhere!
    NSLog(@"This works fine when triggered from IB button");
    [self makeKeyAndOrderFront:self];
}

-(IBAction)hWindowActivate:(id)sender { 
    [self activateDebugWindow]; 
}
@end

NSObject 子类:

@interface AppController : NSObject {
}
@end

@implementation AppController

// whole bunch of working stuff ...
    NSLog(@"This logs fine when I press my global hotkey");

    hTaskWindow *hwindow;                                      // Nothing happens
    [hwindow makeKeyAndOrderFront:(hwindow)];                  // Here at all

    NSLog(@"This logs fine when I press my global hotkey");
// whole bunch of working stuff ...
@end

目标是从另一个类以编程方式打开 hTaskWindow

  • 我有一个带有热键的菜单快捷方式,直接连接到窗口控制器上的接收器操作makeKeyAndOrderFront,它打开窗口

  • 我有一个连接到hWindowActivateNSButton,它也打开窗口

  • 我有一个全局热键,可以成功记录 NSLog

  • 中的条目

但我想做的是让我的全局热键方法激活在其他类中打开窗口的功能。

目前从 IBAction 调用的不必要的 -(void)activateDebugWindow 只是为了尝试设置一个点,让 XIB 一段代码可以打开同一个窗口。

【问题讨论】:

  • 您要在现有窗口上调用makeKeyAndOrderFront 还是要创建一个新窗口?
  • @Willeke 在现有窗口上调用 makeKeyAndOrderFront。初始化完成,我将修改代码以包含更多内容。
  • @Willeke 你现在可以看到更多的窗口代码。如果我在自定义项中添加 [self makeKeyAndOrderFront:NULL]; 行,则窗口会在应用程序启动时启动。目前,activateDebugWindow 方法可以打开窗口,但前提是使用菜单项或按钮的界面构建器连接调用。
  • 哪个对象是窗口的所有者?
  • @Willeke 我认为的窗口控制器,它不是子类

标签: objective-c cocoa


【解决方案1】:

hTaskWindow 应该在应用程序的 xib 中。在应用程序委托中添加一个插座以连接到hTaskWindowhWindowActivate 应该在应用程序委托中。您可以使用[[NSApp delegate] hWindowActivate:sender] 从任何地方激活窗口。

-(IBAction)hWindowActivate:(id)sender { 
    [self.hTaskWindow makeKeyAndOrderFront:sender];
}

将 IB 中的菜单项和按钮连接到应用代理或第一响应者和hWindowActivate:

【讨论】:

    【解决方案2】:

    在调用makeKeyAndOrderFront: 之前,您必须加载要显示的窗口。它是通过 NSWindowController 完成的。

    更改这些行

    hTaskWindow *hwindow;
    [hwindow makeKeyAndOrderFront:(hwindow)];
    

    hTaskWindow *hwindow = [[hTaskWindow alloc] init]; // initialize window here
    NSWindowController *windowController = [[NSWindowController alloc] initWithWindow:hwindow];
    [windowController.window makeKeyAndOrderFront:self];
    

    如果您的窗口是从 .xib 文件加载的,您应该使用initWithWindowNibName: 方法来初始化窗口控制器。

    【讨论】:

    • 感谢您的帮助,现在我实际上已经尝试了与此非常相似的东西(现在是此),我得到了相同的结果 - use of undeclared identifier 'self'
    • 这是一个不寻常的错误,只有在您有语法错误时才会发生。
    猜你喜欢
    • 2015-06-10
    • 2011-12-30
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多