How can I set my window title in Cocoa?

I have a simple cocoa project, it has the default files like AppDelegate.m, AppDelegate.h , ViewController.h and ViewController.m what I want is to set the Window title to the dimension of the current window and update it as the user resizes it.

My Storyboard has an Application Scene, a Window Controller Scene and a test Scene which contains my view:

how do I go about this? should my ViewController or AppController implement NSWindowDelegate ?

Answered by Engineer in 804011022

Hello @aynas, if you'd like to change the default window title, you click the upper-right corner button on Xcode's main window to expand the Inspectors view. After selecting the window, you can use the Title text box to provide a new default name:

There are different ways to change the title of the window programmatically. One straightforward option is to access the window property from your view controller's view and change the title:

- (void)viewDidAppear {
    [super viewDidAppear];
    self.view.window.title = @"My title";
}

Hello @aynas, if you'd like to change the default window title, you click the upper-right corner button on Xcode's main window to expand the Inspectors view. After selecting the window, you can use the Title text box to provide a new default name:

There are different ways to change the title of the window programmatically. One straightforward option is to access the window property from your view controller's view and change the title:

- (void)viewDidAppear {
    [super viewDidAppear];
    self.view.window.title = @"My title";
}

Hey, thanks for the response!

But what if I wanted to change the title as the user resizes the window? In that case viewDidAppear won't cut it.

Also I did try to implement the NSWindowDelegate in the ViewController:

@interface ViewController : NSViewController<NSWindowDelegate>

but the windowDidResize never gets called, what gives?

Edit

Ok turns out I managed to do it.

I just had to set the delegate in the viewDidAppear method:

- (void)viewDidAppear 
{
	[super viewDidAppear];
	//self.view.window.title = @"My title";

	self.view.window.delegate = self;
}
How can I set my window title in Cocoa?
 
 
Q