- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试从其他类更改我的窗口内容,这是 NSViewController 的子类。我正在尝试下面的代码,但它没有做任何事情。
[NSApplication sharedApplication]mainWindow]setContentView:[self view]]; //code in NSViewController
[NSApplication sharedApplication]mainWindow] // returns null
我尝试添加
[window makeMainWindow];
在 App Delegate 类中,但它无济于事。
我错过了什么吗?
附言此外,我正在使用下面的代码来调用我类(class)中的任何委托(delegate)函数,
[(appDelegate *) [[NSApplication sharedApplication]delegate]MyMethod];
但我想知道是否有更好的方法,无需导入委托(delegate)类。像这样的东西
[[NSApplication sharedApplication]delegate]MyMethod];
(它给出警告)
最佳答案
对于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.
我刚刚创建了一个快速测试应用程序并放置了以下代码:
NSLog(@"%@", [[NSApplication sharedApplication] mainWindow]);
进入我的 applicationDidFinishLaunching:aNotification
方法,并进入我连接到应用程序主窗口中的按钮的操作方法。
启动时,mainWindow
为 nil,但当我单击按钮时(在一切启动并运行并显示之后),mainWindow
不再为 nil。
NSApplication
提供了其他可能对您有用的方法:
- windows
- 所有窗口的数组;– keyWindow
- 给出接收键盘输入的窗口(或 nil);– windowWithWindowNumber:
- 返回与窗口编号相对应的窗口 - 如果您知道要替换其内容的窗口的编号,则可以使用它;– makeWindowsPerform:inOrder:
- 向每个窗口发送一条消息 - 您可以使用它来测试每个窗口,看看它是否是您感兴趣的窗口。关于调用delegate
上的方法,你所说的警告对我来说很好。例如,这没有警告:
NSLog(@"%@", [[[NSApplication sharedApplication]delegate] description]);
您收到的警告到底是什么?您是否正在尝试调用不存在的方法?
关于objective-c - 如何从其他类(NSViewController 的子类)获取主窗口(App Delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620251/
我是一名优秀的程序员,十分优秀!