windowNibName error in Swift / Cocoa?
Now type of windowNibName
replace to struct
from String
.
You can check more details here.
To fix error you can use below code :
class MainWindowController: NSWindowController {
override var windowNibName: NSNib.Name? {
return NSNib.Name("MainWindowController")
}
}
If -initWithWindowNibPath:owner: was used to initialize the instance, this gives the last path component with its extension
stripped off. If -initWithWindowNibName:[owner:] was used this just
gives that name.
open var windowNibName: NSNib.Name? { get }
How can I pass NSNib.Name to a window controller in Swift/Cocoa
You are mixing up lowercase and uppercase notation. If you are using the same name for class and variable be careful how to write it, case sensitivity matters.
To initialize a class you have to use the uppercase name.
self.mainWindowController = MainWindowController()
mainWindowController?.showWindow( self )
How to initialize a NSWindowController in Swift?
NSWindowController
has 2 designated initializers:
init(window: NSWindow!)
init(coder: NSCoder!)
When creating a subclass, you should invoke the designated initializer of its superclass. Recent versions of Xcode enforce this. Either via built-in language mechanism (Swift) or via NS_DESIGNATED_INITIALIZER
macro (Objective-C).
Swift additionally requires that you call the superclasses designated initializer when you override a convenience initializer.
From the "Initialization: Designated Initializers and Convenience Initializers" section of Swift Programming Guide:
If the initializer you are overriding is a convenience initializer,
your override must call another designated initializer from its own
subclass, as per the rules described above in Initializer Chaining.
In your case, you should probably override init(window: NSWindow!)
and call super's counterpart from there.
What is the correct way to initialize a NSWindowController object in Swift?
myThing = Thing()
can be placed where the property is declared- You should call
super.init
- You'll also need to implement
init?(coder: NSCoder)
, Xcode will happily insert a template for this - Most likely you'll need another initializer that will load the nib:
Something like this:
class MainWindowController: NSWindowController {
// the type of myThing will be inferred by the compiler
let myThing = Thing()
init() {
super.init(window: nil)
// load the contents of the nib, and set the owner as self, which connects the oultlets
NSBundle.mainBundle().loadNibNamed("nibText", owner: self, topLevelObjects: nil)
}
// you might no longer need this intializer
override init(window: NSWindow!) {
super.init( window: window )
}
// ...
}
Programmatic window creation in -[NSWindowController loadWindow] not working
as @Willeke points out, loadWindow will only be called for nib
but since you're calling showWindow, you could override that
import Cocoa
class MainWindowController: NSWindowController {
override func loadWindow() {
print("loadWindow")
}
override func showWindow(_ sender: Any?) {
print("showWindow")
self.window = NSWindow(contentRect: NSMakeRect(100, 100, 100, 100), styleMask: [.titled, .resizable, .miniaturizable, .closable], backing: .buffered, defer: false)
window?.makeKeyAndOrderFront(sender)
}
}
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var mwc: MainWindowController!
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.mwc = MainWindowController(window: nil)
self.mwc.showWindow(nil)
}
static func main() {
let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps: true)
app.run()
}
}
Create NSWindow from nib name
It's bug, I think.
Create your own init method and load xib file yourself, like this:
class MyWindowController: NSWindowController {
init() {
super.init(window: nil)
/* Load window from xib file */
NSBundle.mainBundle().loadNibNamed("MyWindow", owner: self, topLevelObjects: nil)
}
}
Correct way to create nswindow, using Swift and Cocoa
This might a be a job for lazy
class AppDelegate : NSApplicationDelegate {
lazy var windowController = WindowTestController(windowNibName: "Window")
@IBAction func buttonClicked(sender : AnyObject) {
windowController.showWindow(sender)
}
}
self.windowController
will be neither allocated nor nil until you attempt to call it, at which time it will be inited. But not until that time.
Swift modal sheet: willPositionSheet does doesn't get called
2 things.
First: Your view controller view
's window
property likely won't be nonnil until viewDidAppear()
(you can verify by debugging with breakpoints whether the window is nonnil). So make sure that the delegate is actually set on the window. I'd also note that you might want to try leaving the window delegate tied to the window's controller object or a distinct window delegate object that is not owned by the view controller. The view controller is sort of an odd place to put this logic.
Second: Presenting a window that is owned by an NSWindowController
doesn't seem to get these callbacks for some reason (I tested with just passing a standard NSWindow
object and the delegate received the willPositionSheet
call). Not sure why this is the case, but something with how these windows are setup I guess. With that said, you can try using the newer (10.10) NSViewController
presentation APIs to make your life easier. I would try presenting an NSViewController
using NSViewController
's presentViewControllerAsSheet(_:)
.
So in summary you could have something like:
override func viewDidAppear() {
super.viewDidAppear()
self.view.window?.delegate = self
}
and for presenting:
let vc = SheetViewController() // Note this is a view controller, not window controller
presentViewControllerAsSheet(vc)
Hopefully that helps a bit.
Related Topics
How to Detect If The User Was Deleted from Firebase Auth
Convert/Wrap Swift Struct as Nsvalue for Caanimation Purposes
Detect When a Custom Cell Is Selected from Within The Cell Itself
Apple Turicreate Always Return The Same Label
Facebook Graphrequest for Swift 5 and Facebook Sdk 5
Make Tabview Background Transparent
Difference Between @Propertydelegate and @Propertywrapper
Convert from Nsdictionary to [String:Any]
Difference When Declaring Swift Protocol Using Inheritance from Another Protocol or Using Where Self
Does Cocoa Connection Binding to Nstoolbaritem Prevent Deinitializing
Why Is Swift Counting This Grapheme Cluster as Two Characters Instead of One
Why Do I Get Source Kit Service Terminated Error
Enum Named 'Type' in Nested Class Fails
Swift Switch Case Compiler Error
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
What's The Difference Between [String] VS. [(String)]