1

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)
}
Béatrice Cassistat
  • 1,048
  • 12
  • 37
  • I am not familiar with that problem, but in the referenced thread it is said that you should *subclass* NSWindow, not define an extension (which works in Swift as well according to this comment: https://stackoverflow.com/questions/7561347/cant-edit-nstextfield-in-sheet-at-runtime#comment46992411_12363406) – Martin R Jun 14 '17 at 19:08
  • Yes you are right, the extension approach was used (in Obj-C) on another thread. Because I don't know how I can subclass a NSWindow when it is created by NSViewController.presentViewControllerAsSheet(...). – Béatrice Cassistat Jun 14 '17 at 19:19
  • Are you using storyboards, xibs, or a mixture? – Paul Patterson Jun 14 '17 at 19:56
  • Only storyboards. But I am calling presentViewControllerAsSheet programmatically instead of a segue. – Béatrice Cassistat Jun 14 '17 at 20:04
  • So where is your sheet? In the storyboard? If so, how is it connected to the other storyboard elements? – Paul Patterson Jun 14 '17 at 20:33
  • @PaulPatterson In a storyboard (it's a NSPageController) and it's created using its Storyboard ID. – Béatrice Cassistat Jun 14 '17 at 21:24
  • 1
    Sorry, I can't replicate your problem. [This project](http://jmp.sh/AQHR8e6) replicates the set-up you've described, but it behaves as desired. Have a look at it, and see if you can figure out where you're going wrong. – Paul Patterson Jun 14 '17 at 22:36
  • You are right. I created in my project another NSTextField and this one was editable. I don't understand because the configuration of the two NSTextField were the same in the identity and attribute inspectors. I replaced the problematic NSTextField and remapped the outlet with my code and now it works. Strange problem. Thanks a lot for your help! – Béatrice Cassistat Jun 15 '17 at 15:47

1 Answers1

2

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)
}
Paul Patterson
  • 6,840
  • 3
  • 42
  • 56
  • I am not using XIB files, but it looks similar to what I am doing. But my problem is that the NSTextField in the controller is not editable when showing in a sheet. – Béatrice Cassistat Jun 14 '17 at 21:22