Programmatically change title of window created by storyboard
Asked Answered
A

2

5

In xcode 6.1, how do i programmatically change the title of the window created by storyboard? This project is for OS X. By default, it is "Window". I am using OBJ C.

Advised answered 8/1, 2015 at 20:14 Comment(0)
G
15

Code in Swift is

override func viewDidAppear() {
    self.view.window?.title = "joe"
}

and in Objective-C it's approximately self.view.window.title = @"joe";

It has to be in the viewDidAppear because in the viewDidLoad it's too early.


OT, but to do it via Storyboards it's here:

enter image description here

Gun answered 13/1, 2015 at 22:37 Comment(4)
So you don't have to subclass the NSWindow or NSWindowController?Advised
You don't have to subclass NSWindow at all, and you probably should not.Gun
The code version is in the viewDidAppear for the primary view controller.Gun
How can I access the app window in AppDelegate ?Guenzi
R
0

In the AppDelegate context of Apple's common Objective-C Xcode templates for macOS, you can access your main window handle through the following line of code:

[[[NSApplication sharedApplication] mainWindow] setTitle:@"Your Title"];

Or just use the global constant shorthand for the shared app instance:

[[NSApp mainWindow] setTitle:@"Your Title"];

These examples first retrieve the app's instance and then get hold of it's main window. Note that if the app’s storyboard or NIB file has not yet finished loading, the app is inactive or hidden, the title might not get applied.

The above assumes you have a Main.storyboard with hierarchy Window Controller Scene > Window Controller > Window -- with non-custom Identifier==Automatic and not subclassed this NSWindow.

Rosewood answered 3/1, 2021 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.