I had code similar to this which was causing the error "-[NSWindow release]: message sent to deallocated instance":
var w = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .closable, backing: NSWindow.BackingStoreType.buffered, defer: false)
w.title = "Window"
w.makeKeyAndOrderFront(nil)
//w.isReleasedWhenClosed = false
w.close()
w = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .closable, backing: NSWindow.BackingStoreType.buffered, defer: false)
When I added the commented line it worked fine.
I just wanted to know if this is expected behaviour with the way Swift does memory management?
It was quite unexpected that the variable reassignment could be causing this problem.
Related
I currently have:
let window = NSWindow(contentRect: NSRect(x: 300, y: 300, width: 200, height: 200), styleMask: [.borderless], backing: .buffered, defer: true)
window.backgroundColor = NSColor.green
window.level = .floating
window.collectionBehavior = [.stationary, .canJoinAllSpaces, .fullScreenAuxiliary]
window.makeKeyAndOrderFront(nil)
Current behaviour:
When space changes (moving between full screen windows) the window moves relative to the desktop/space .
Expected behaviour:
I want the window to behave much like the notifications window in macOS, where the window "floats" above the screen even when active space changes.
I am new to MacOS development and Swift so sorry if this is a basic/stupid question.
I am getting this error:
Cannot find type 'UIApplicationDelegate' in scope
I have created an AppDelegate.swift file to try to define a default window size for my app. I have this code:
import Foundation
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: ContentView())
window.makeKeyAndOrderFront(nil)
}
I'm not really sure what to do here. Is there something I need to import?
Thnak you for your help.
import FBSDKCoreKit. This will help u :)
I am creating a second window in my SwiftUI app for Mac.
This is how I call my second window.
let window = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Add Person")
window.contentView = NSHostingView(rootView: AddPerson())
window.makeKeyAndOrderFront(nil)
This works fine. However, when I try to close that window, the app is crashing.
I am calling this, to close the window.
NSApplication.shared.keyWindow?.close()
I think there is a problem with opening two windows. Is my opening call correct?
Edit: I need to set the window.number when I am creating that window. How can I set that? I haven't found anything.
Well, I don't think it is because of window itself (I tested your case on Xcode 11.3 with just simple text & button content and it works), but due to some content and/or active objects (managers, etc.)
Anyway, you can try instead of force close (as in provided snapshot) to close via action
NSApp.keyWindow?.performClose(nil)
it does the same as clicking close button on window's titlebar.
Update: store below window as member, not a local variable
let window = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
like main window
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow! // << default, main window
var window2: NSWindow! // << other window (as example)
... // somewhere below
window2 = NSWindow(contentRect: NSRect(x: 20, y: 20, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
I have the following code:
class EditorWindow: NSWindow {
#Binding var keycode : Int
override func keyDown(with event : NSEvent) {
super.keyDown(with: event)
Swift.print("Caught a key down: \(event.keyCode)!")
}
init(keycode : Int){
self.keycode = keycode
super.init(contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
}
}
Placing the self.keycode = keycode before the super.init gives me the error "'self' used in property access 'keycode' before 'super.init' call", (as in this question, which suggests to swap the order). If I swap the order, I get the error: "Property 'self.keycode' not initialized at super.init call" (as in this question which suggests the original order as the solution) - it seems whichever order I use I get an error - how do I get around this?
You need to pass a Binding<Int> to the constructor:
init(keycode : Binding<Int>){
self._keycode = keycode
super.init( // .....
}
I'd like to create a new window programmatically. I have the following code, and it builds, but my window doesn't show up. The only way I can make it visible is by adding it as a child window to the default 'window'. How can I make 'win' be an independent window?
#IBOutlet var window: NSWindow
func applicationDidFinishLaunching(aNotification: NSNotification?) {
var win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
styleMask: NSResizableWindowMask,
backing: NSBackingStoreType.Buffered, defer: true)
window.addChildWindow(win, ordered:NSWindowOrderingMode.Above)
}
What about adding:
win.makeKeyAndOrderFront(win)
For me on OSX (not iOS) using Swift and writing in vim
let win = NSWindow(contentRect: NSMakeRect(100, 100, 600, 200),
styleMask: NSResizableWindowMask,
backing: NSBackingStoreType.buffered, defer: true)
win.makeKeyAndOrderFront(win)
pops up a window
You also need a NSWindowController to display the window:
let window = NSWindow(...)
let controller = NSWindowController(window: window)
controller.showWindow(self)