简体   繁体   中英

Uneditable NSTextField in sheet

I have a NSTextField in a sheet and it is not editable.

This bug seems known in Cocoa, see: can't edit NSTextField in sheet at runtime

But I am looking for a solution in pure Swift. I've tried this:

extension NSWindow {
    override var canBecomeKey:Bool {
        get {
            return true;
        }
    }
}

But it seems like we can't override class properties. Then I tried setting a title to the window, or calling makeKey() and becomeKey() on the sheet main ViewController's viewDidAppear function, setting a title and title visibility did not helped either...

Edit: Here's the code I use for creating the sheet:

@IBAction func addDataSourceAction(_ sender: Any) {
    let sheetController = NSStoryboard.init(name: "MyStoryboard", bundle: nil).instantiateController(withIdentifier: "MyPageController") as! NSPageController
    presentViewControllerAsSheet(sheetController)
}

Assuming you're using macOS 10.10+, you don't need NSWindow to create sheets. Just create an NSViewController subclass, use Interface Builder to design the accompanying view, then present the sheet from a separate view controller:

// MainViewController.swift
@IBAction func showSheet(_ sender: Any) {
    let sheetController = SheetViewController() // Assumes there's a SheetViewController.xib file
    presentViewControllerAsSheet(sheetController)
} 

When you want to get rid of the sheet, simply dismiss it:

// SheetViewController.swift
@IBAction func endSheet(_ sender: Any) {
    guard let _ = presenting else { fatalError("No presenting controller!") }
    presenting!.dismissViewController(self)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM