3 Replies
      Latest reply on Oct 21, 2019 5:32 AM by Claude31
      narcisfromgirona Level 1 Level 1 (0 points)

        If I want to refer to the actual window (where I write the code) I would do view.window or self.view.window. For instance:

        view.window?.title = "new title"

         

        But if I am in another place, for instance, in the AppDelegate.swift how do I refer to a specific window? For instance, I tried and it does not work:

        ViewController.title = "new title"

         

        (macOS and Swift)

        (I am quite new with Swift. Sorry if my question is innocent but I cannot find the answer anywhere. I would appreciate any orientation)

        • Re: How to call a window
          Claude31 Level 8 Level 8 (6,865 points)

          ViewController.title = "new title"

           

          You noticed this did not work.

          Because you need to apply to an instance, not to the class.

           

          You have several options here.

           

          1. Brute force (not recommended):

          - declare a global var (outside of any class)

               var myController : UIViewController?

          - set it when you create the VC (for instance when you load the View in its viewDidLoad

          - use as

               myWindow?.title = "new title". // In case it is nil

           

          2. Use notification

          In ViewController:

          - make VC observe some notification (addObserver) : "notifyTitleChange"

          - as a handling of notification, change the title

               self.view.window?.title = "new title"

           

          In AppDelegate

          - post a notification when needed

          - take care: if you send when VC not yet created, notification may be lost.

           

          3. Use delegation

            • Re: How to call a window
              narcisfromgirona Level 1 Level 1 (0 points)

              Ok. I could manage to put a notification in the AppDelegate.swift:

              func applicationDidFinishLaunching(_ aNotification: Notification) {

                      NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.spaceChange), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)

              }

              @objc func spaceChange() {

                      print("space did change")

                      // window?.setFrame(NSRect(x:0,y:0,width:500,height:500), display: true)

              }

               

              It prints well the message, so it detects when the user changes to a different space. Now I want to put a window in the active space, change its title or do things with that window. My question is how do I call that window

                • Re: How to call a window
                  Claude31 Level 8 Level 8 (6,865 points)

                  I would put the observer as well as spaceChange() in the ViewController for which you want to change the title.

                   

                  And post a notification from the appDelegate or in any controller from where you want to change the title of another view (or its own view also).