Reputation: 131
I have a Cocoa application with a secondary window created using a subclass of NSWindowController. I wish to set the window title. The documented method call is 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?
Upvotes: 13
Views: 16464
Reputation: 8988
I had the same issue with a non-document based app using the latest Xcode when binding the title to a selected objects filename. 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.
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. If it's not nil then I set the property to the selected objects filename.
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)
}
}
Upvotes: 0
Reputation: 1379
I just use
self.window?.title = "Some String"
where I create the window.
Upvotes: 4
Reputation: 20
In Swift this is can be done with: 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.
Upvotes: 0
Reputation: 10198
You can connect Your window with IBOutlet and then change Your code:
[[self window] setTitle:@"test string"];
To this:
[yourWindow setTitle:@"test string"];
Full code for example:
.h
IBOutlet NSWindow *yourWindow; //Don't forget to connect window to this
.m
-(void)awakeFromNib {
[yourWindow setTitle:@"test string"];
}
Title can be changed in Attributes inspector:
Upvotes: 18
Reputation: 674
The NSWindowController class reference indicates that to customize the title, you should override the windowTitleForDocumentDisplayName:
method.
Upvotes: 5