OK, here is the complete setup. I tested. May probably be optimized, but that's to show.
0. In the storyboard, I have 2 windowControllers, each with a viewController.
The second window controllr has an Identifier : S1W2
In the first window, I have a button that will call the second window and show with fadeIn.
1. In the VewController file for the FIRST window, define a var for the secondWindowController:
import Cocoa
class ViewController: NSViewController {
var secondWindowController: NSWindowController? // could also be defined outside the class
2. Create a button in this VC of First window in the storyboard
No IBOutlet needed
3. Create the fadeIn function in class ViewController:
func fadeInMyWindow() {
print("Starts fade") // Just to check it's called
if self.secondWindowController == nil || self.secondWindowController!.window == nil { return } // Better be cautious
DispatchQueue.main.async { // UI
self.secondWindowController!.window!.alphaValue = 0.0
self.secondWindowController!.window!.makeKeyAndOrderFront(nil)
}
let centiemeSeconde = UInt32(10_000) // Can adjust the fade in speed
for i in 0...99 {
usleep(centiemeSeconde) // I wait 1/100 second to have a visible effect
DispatchQueue.main.async { // UI ;
self.secondWindowController!.window!.alphaValue = CGFloat(i) / 100
}
}
}
4. Connect the button to its IBAction from stroyboard to class ViewController ; connection is done by this action, bnot through a segue:
@IBAction func ShowWindowByCode(_ sender: NSButton) {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let windowController = storyboard.instantiateController(withIdentifier: "S1W2") as? NSWindowController
{
secondWindowController = windowController
DispatchQueue.global(qos: .userInitiated).async { // Needed for animation to occur
self.fadeInMyWindow()
}
}
}
Hope that's clear.