如何从其他类(NSViewController的子类)获取主窗口(应用程序委托)? [英] How to get Main Window (App Delegate) from other class (subclass of NSViewController)?

查看:198
本文介绍了如何从其他类(NSViewController的子类)获取主窗口(应用程序委托)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的Windows内容,从其他类,这是NSViewController的子类。我尝试下面的代码,但它不做任何事情。

  [NSApplication sharedApplication] mainWindow] setContentView:[self view]]; // NSViewController中的代码

[NSApplication sharedApplication] mainWindow] //返回null

我试图添加

  [window makeMainWindow];应用程序代理类中的

,但不会有帮助。



我错过了什么吗?



还有我使用下面的代码调用任何委托函数在我的类,

  [(appDelegate *)[[NSApplication sharedApplication] delegate] MyMethod]; 

但我不知道有没有更好的东西,wihtout导入委托类。类似这样的

  [[NSApplication sharedApplication] delegate] MyMethod]; 

解决方案


$ b 这个方法可能返回nil,如果


我刚刚创建了一个应用程序的nib文件没有完成加载,如果接收者没有激活,快速测试应用程序,我放置了以下代码:

  NSLog(@%@,[[NSApplication sharedApplication] mainWindow] ; 

到我的 applicationDidFinishLaunching:aNotification 到我的应用程序的主窗口中的一个按钮。



在启动时, mainWindow 是nil,但是当我点击按钮(一切都开始运行并显示后), mainWindow 不再为nil。



NSApplication 提供您可能对您有用的其他方法:




  • - windows - 所有窗口的数组;

  • - keyWindow - 给出正在接收键盘输入(或nil)的窗口;

  • - windowWithWindowNumber: - 返回与窗口编号 - 如果您知道您要替换的窗口的编号,您可以使用此窗口编号;

  • - makeWindowsPerform:inOrder: - 向每个窗口发送一条消息 - 您可以使用它来测试每个窗口,看看它是否是您感兴趣的窗口。



关于委托的调用方法,你说的警告对我来说工作正常。例如,这没有警告:

  NSLog(@%@,[[[NSApplication sharedApplication] delegate] ]); 

您收到的警告到底是什么?您尝试调用不存在的方法?


I'm trying to change my windows content, from other class , that is subclass of NSViewController.I'm trying code below, but it doesn't do anything.

[NSApplication sharedApplication]mainWindow]setContentView:[self view]]; //code in NSViewController

[NSApplication sharedApplication]mainWindow] // returns null

I tried to add

[window makeMainWindow];

in App Delegate class, but it won't help.

Did I miss something?

P.S. Also I'm using code below to call any delegate function in my class,

 [(appDelegate *) [[NSApplication sharedApplication]delegate]MyMethod];

but I wonder is there something better, wihtout importing delegate class. Something like this

[[NSApplication sharedApplication]delegate]MyMethod];

(it gives warning)

解决方案

For the mainWindow method the docs say:

This method might return nil if the application’s nib file hasn’t finished loading, if the receiver is not active, or if the application is hidden.

I just created a quick test application and I placed the following code:

NSLog(@"%@", [[NSApplication sharedApplication] mainWindow]);

into my applicationDidFinishLaunching:aNotification method, and into an action method which I connected to a button in the main window of my application.

On startup, the mainWindow was nil, but when I click the button (after everything is up and running and displayed), the mainWindow was no longer nil.

NSApplication provides other methods which you may be useful to you:

  • - windows - an array of all the windows;
  • – keyWindow - gives the window that is receiving keyboard input (or nil);
  • – windowWithWindowNumber: - returns a window corresponding to the window number - if you know the number of the window whose contents you wish to replace you could use this;
  • – makeWindowsPerform:inOrder: - sends a message to each window - you could use this to test each window to see if it's the one you are interested in.

With regard to calling methods on the delegate, what you say gives a warning works fine for me. For example, this works with no warnings:

NSLog(@"%@", [[[NSApplication sharedApplication]delegate] description]);

What exactly is the warning you receive? Are you trying to call a method that doesn't exist?

这篇关于如何从其他类(NSViewController的子类)获取主窗口(应用程序委托)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆