[英]Cocoa: How to set window title?
I have a Cocoa application with a secondary window created using a subclass of NSWindowController.我有一个 Cocoa 应用程序,它带有一个使用 NSWindowController 的子类创建的辅助窗口。 I wish to set the window title.
我想设置窗口标题。 The documented method call is setTitle:.
记录的方法调用是 setTitle:。 I have called this from within the window controller as follows:
我在窗口控制器中调用了它,如下所示:
- (void)windowDidLoad
{
// set window title
[[self window] setTitle:@"test string"];
}
This does not affect the title of the window though.但这不会影响窗口的标题。
Any suggestions please?请问有什么建议吗?
You can connect Your window with IBOutlet and then change Your code:您可以将您的窗口与 IBOutlet 连接,然后更改您的代码:
[[self window] setTitle:@"test string"];
To this:对此:
[yourWindow setTitle:@"test string"];
Full code for example:完整代码例如:
.h 。H
IBOutlet NSWindow *yourWindow; //Don't forget to connect window to this
.m .m
-(void)awakeFromNib {
[yourWindow setTitle:@"test string"];
}
Title can be changed in Attributes inspector :标题可以在属性检查器中更改:
NSWindowController 类参考表明要自定义标题,您应该覆盖windowTitleForDocumentDisplayName:
方法。
I just use我只是用
self.window?.title = "Some String"
where I create the window.我在哪里创建窗口。
In Swift this is can be done with: someOutlet.title = "New Title"
在 Swift 中,这可以通过以下方式完成:
someOutlet.title = "New Title"
Here is an example that lives in your window controller class:这是一个存在于您的窗口控制器类中的示例:
@IBOutlet weak var contentOutlet: NSWindow!
override func windowDidLoad() {
super.windowDidLoad()
contentOutlet.title = "New Title"
}
Again, remember to connect the outlet to the window, or just drag an outlet from the window to your window controller class.同样,记住将插座连接到窗口,或者只是将插座从窗口拖到您的窗口控制器类中。
I had the same issue with a non-document based app using the latest Xcode when binding the title to a selected objects filename.在将标题绑定到选定对象文件名时,我在使用最新 Xcode 的非基于文档的应用程序中遇到了同样的问题。 Whenever there is no selection the title shows up as "Untitled" - which makes no senses in the application.
只要没有选择,标题就会显示为“无标题”——这在应用程序中没有意义。
So I created two properties in the NSWindowController - one gets bound to the selected object's filename and the other gets bound to the Window title.所以我在 NSWindowController 中创建了两个属性——一个绑定到所选对象的文件名,另一个绑定到窗口标题。
In the didSet{} method of the property bound to the selected object I check for nil value and set the title property to the application name.在绑定到所选对象的属性的 didSet{} 方法中,我检查 nil 值并将 title 属性设置为应用程序名称。 If it's not nil then I set the property to the selected objects filename.
如果它不是 nil,那么我将该属性设置为所选对象的文件名。
class WindowController: NSWindowController {
/// Bind this to the controllers selected objects filename property
@objc dynamic var filename: String? {
didSet {
if let name = filename {
title = name
} else {
title = "IAM2 - no selection"
}
}
}
/// Bind this to the windows title
@objc dynamic var title: String = "IAM2"
override func windowDidLoad() {
super.windowDidLoad()
self.bind(NSBindingName(rawValue: "filename"),
to: self.controller,
withKeyPath: "selectedObject.filename",
options: nil)
self.window?.bind(NSBindingName(rawValue: "title"),
to: self,
withKeyPath: "title",
options: nil)
self.window?.bind(NSBindingName(rawValue: "subtitle"),
to: controller,
withKeyPath: "rootPath",
options: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.