Objective c Cocoa:如何设置窗口标题?

Objective c Cocoa:如何设置窗口标题?,objective-c,cocoa,Objective C,Cocoa,我有一个Cocoa应用程序,使用NSWindowController的子类创建了一个辅助窗口。我想设置窗口标题。记录的方法调用是setTitle:。我已从窗口控制器中调用此函数,如下所示: - (void)windowDidLoad { // set window title [[self window] setTitle:@"test string"]; } 但这不会影响窗口的标题 有什么建议吗?NSWindowController类参考指出,要自定义标题,您应该覆盖win

我有一个Cocoa应用程序,使用NSWindowController的子类创建了一个辅助窗口。我想设置窗口标题。记录的方法调用是setTitle:。我已从窗口控制器中调用此函数,如下所示:

- (void)windowDidLoad
{
    // set window title
    [[self window] setTitle:@"test string"]; 
}
但这不会影响窗口的标题


有什么建议吗?

NSWindowController类参考指出,要自定义标题,您应该覆盖
windowTitleForDocumentDisplayName:
方法。

您可以将窗口与IBOutlet连接,然后更改代码:

[[self window] setTitle:@"test string"];
对此:

[yourWindow setTitle:@"test string"];
完整代码,例如:

h

m



当然,您可以不通过编程方式更改标题:

[yourWindow setTitle:@"test string"];
可以在属性检查器中更改标题:


在Swift中,这可以通过:
someOutlet.title=“New title”

下面是一个存在于窗口控制器类中的示例:

@IBOutlet weak var contentOutlet: NSWindow!

override func windowDidLoad() {
    super.windowDidLoad()

    contentOutlet.title = "New Title"
}
同样,请记住将插座连接到窗口,或者将插座从窗口拖动到窗口控制器类。

我只是使用

self.window?.title = "Some String"

创建窗口的地方。

在将标题绑定到选定对象文件名时,使用最新Xcode的非基于文档的应用程序也存在同样的问题。当没有选择时,标题显示为“Untitled”-这在应用程序中没有意义

因此,我在NSWindowController中创建了两个属性——一个绑定到选定对象的文件名,另一个绑定到窗口标题

在绑定到所选对象的属性的didSet{}方法中,我检查nil值并将title属性设置为应用程序名称。如果不是nil,那么我将属性设置为selectedobjects文件名

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)
    
}

}

这个方法被调用了吗?你不需要这样做
NSWindowController
已经有一个
窗口
插座,你只需要连接它。我用那个插座(见初始帖子)试过,没有任何影响。非常感谢。使用另一个插座而不是NSWindowController中的默认“窗口”功能正常。有人能解释一下这种行为吗?我为浪费人们的时间而道歉,最好用在其他地方,我没有在IB中连接窗口。再次道歉
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)
    
}

}