Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: How to set window title?

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?

like image 770
tobin Avatar asked Feb 23 '12 04:02

tobin


3 Answers

I just use

self.window?.title = "Some String"

where I create the window.

like image 118
Peter Ahlberg Avatar answered Oct 15 '22 19:10

Peter Ahlberg


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"];
}



And of course You can change title not programatically:

Title can be changed in Attributes inspector:

enter image description here

like image 34
Justin Boo Avatar answered Oct 15 '22 21:10

Justin Boo


The NSWindowController class reference indicates that to customize the title, you should override the windowTitleForDocumentDisplayName: method.

like image 5
Chaosphere2112 Avatar answered Oct 15 '22 20:10

Chaosphere2112