为什么这个窗口根本不显示?

问题描述 投票:0回答:1

我试图了解如何在两个不同的视图或函数之间更改变量(var 或标签)中的数据。

我正在从

swiftUi
迁移,我被用来在整个应用程序中创建
stateobject and accessing the variable through environment variables
。但现在可可框架让我有点困惑。

所以我创建了两个视图

CustomView1
CustomView2
,其中一个包含一个按钮,另一个包含一个标签。因此,当单击按钮时,标签的字符串值应该更改。

但问题是运行时什么也没有显示。连

window
都没有出现。

错误:没有错误

main.swift


import Cocoa

let app = NSApplication.shared
 
let appDelegate = App2()
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()

App2.swift

import Cocoa
 
 
class App2: NSObject, NSApplicationDelegate {
    var window:NSWindow!
    var view: NSView!
    var customView1: CustomView1!
    var customView2: CustomView2!
    
    
    
    func buildMenu() {
        let mainMenu = NSMenu()
        NSApp.mainMenu = mainMenu
        // **** App menu **** //
        let appMenuItem = NSMenuItem()
        mainMenu.addItem(appMenuItem)
        let appMenu = NSMenu()
        appMenuItem.submenu = appMenu
        appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
    }
    
    func buildWnd() {
        let _wndW: CGFloat = 400
        let _wndH: CGFloat = 400
        //@State var title: String = "app title"
        let appTitle: String = "app title"
        
        
        window = NSWindow(contentRect: NSMakeRect(0, 0, _wndW, _wndH), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
        window.center()
        window.title = appTitle
        window.makeKeyAndOrderFront(window)
        
        // Create a label
        let labelFrame = NSRect(x: 50, y: 50, width: 200, height: 30)
        let labelText = "Hello, Cocoa!"
        
        
        customView1 = CustomView1(frame: NSRect(x: 10, y: 50, width: 120, height: 50))
        customView2 = CustomView2(frame: NSRect(x: 10, y: 100, width: 120, height: 50))
        
        window.contentView?.addSubview(customView1)
        window.contentView?.addSubview(customView2)
        
        
    }
    
    
    class CustomView1: NSView {
        let customView2 = CustomView2(frame: NSRect(x: 10, y: 50, width: 100, height: 30))
        
        func createButton(frame: NSRect, title: String) -> NSButton {
            let button = NSButton(frame: frame)
            button.title = title
            button.bezelStyle = .rounded
            button.target = self
            button.action = #selector(buttonClicked(_:))
            return button
        }
        
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
            
            let button = createButton(frame: NSRect(x: 10, y: 10, width: 100, height: 30), title: "Click me!")
            self.addSubview(button)
            self.addSubview(customView2)
        }
        
        @objc func buttonClicked(_ sender: NSButton) {
            customView2.label.stringValue = "Button clicked!"
        }
    }
    
    class CustomView2: NSView {
        let label = NSTextField(frame: NSRect(x: 10, y: 10, width: 100, height: 20))
        
        override init(frame frameRect: NSRect) {
            super.init(frame: frameRect)
            label.stringValue = "Hello, world!"
            label.isEditable = false
            label.isSelectable = false
            label.isBezeled = false
            label.drawsBackground = false
            self.addSubview(label)
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
      
    
    func applicationDidFinishLaunching(_ notification: Notification) {
        buildMenu()
        buildWnd()
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
    
}
    
swift cocoa nsview
1个回答
0
投票

以下源代码是您的演示的重新安排。这两个自定义视图需要与 appDelegate 分开。就添加控件而言,自定义视图 2 比自定义视图 1 更正确。您需要使用 Xcode 的 File/New/File 创建一个名为“main.swift”的新文件。将以下代码完整复制/粘贴到该文件中,然后删除预先提供的 AppDelegate,它应该可以正常运行。

import Cocoa
 
var customView1: CustomView1!
var customView2: CustomView2!

class CustomView1: NSView {
    let button = NSButton(frame:NSMakeRect(10,10,100,24))
    
    @objc func buttonClicked(_ sender: NSButton) {
        customView2.label.stringValue = "Button clicked!"
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        button.title = "Click Me"
        button.bezelStyle = .rounded
        button.target = self
        button.action = #selector(buttonClicked(_:))
        self.addSubview(button)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        let bkgrnd = NSBezierPath(rect: dirtyRect)
        NSColor.lightGray.set()
        bkgrnd.fill()
    }
        
}

class CustomView2: NSView {
    let label = NSTextField(frame: NSRect(x: 10, y: 10, width: 100, height: 20))
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        label.stringValue = "Hello, world!"
        label.isEditable = false
        label.isSelectable = false
        label.isBezeled = false
        label.drawsBackground = true
        self.addSubview(label)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ApplicationDelegate: NSObject, NSApplicationDelegate {
    var window:NSWindow!
    var view: NSView!

    func buildMenu() {
        let mainMenu = NSMenu()
        NSApp.mainMenu = mainMenu
        // **** App menu **** //
        let appMenuItem = NSMenuItem()
        mainMenu.addItem(appMenuItem)
        let appMenu = NSMenu()
        appMenuItem.submenu = appMenu
        appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
    }
    
    func buildWnd() {
        let _wndW: CGFloat = 400
        let _wndH: CGFloat = 400
       
        let appTitle: String = "app title"
                
        window = NSWindow(contentRect: NSMakeRect(0, 0, _wndW, _wndH), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
        window.center()
        window.title = appTitle
        window.makeKeyAndOrderFront(window)
        
        // Create a label
        let txtFld = NSTextField (frame:NSMakeRect( 50, 150, 200, 30 ))
        txtFld.isEditable = false
        txtFld.isSelectable = false
        window.contentView!.addSubview(txtFld)
        txtFld.stringValue = "Hello, Cocoa!"
                
        customView1 = CustomView1(frame: NSRect(x: 10, y: 50, width: 120, height: 50))
        customView2 = CustomView2(frame: NSRect(x: 10, y: 300, width: 120, height: 50))
        
        window.contentView!.addSubview(customView1)
        window.contentView!.addSubview(customView2)
    }
            
    func applicationDidFinishLaunching(_ notification: Notification) {
        buildMenu()
        buildWnd()
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
}

let applicationDelegate = ApplicationDelegate()

let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = applicationDelegate
application.activate(ignoringOtherApps:true)
application.run()

© www.soinside.com 2019 - 2024. All rights reserved.