1

I'm working on an app with a custom file browser, and currently I'm working on implementing copy/paste. I have a button in the navigation bar that I've attached a UIMenu to, to reveal options on adding files, including pasting. I want to have the paste button automatically enable when files are in the clipboard, and disable when the clipboard's empty. I tried doing this at first:

// in class definition
var pasteAction: UIAction?
@IBOutlet var addButton: UIBarButtonItem!
// in viewDidLoad()
pasteAction = UIAction(
    title: "Paste Files",
    image: UIImage(systemName: "doc.on.clipboard"),
    attributes: clipboard.isEmpty ? .disabled : [],
    handler: pasteFiles
)
addButton.menu = UIMenu(
    title: "", image: nil, identifier: nil, options: .displayInline,
    children: [folder, file, imp, pasteAction!]
)
// in copy()
pasteAction!.attributes = clipboard.isEmpty ? .disabled : []

However, the paste button remained disabled. (When leaving and re-entering the view controller, the paste button is enabled.) I also tried replacing the menu with a new one with the same children:

pasteAction!.attributes = clipboard.isEmpty ? .disabled : []
var c = Array(addButton.menu!.children[0...2])
c.append(pasteAction!)
addButton.menu = addButton.menu!.replacingChildren(c)

This did not work either. Finally, I tried creating a new UIAction, just in case it's not actually possible to modify it:

var c = Array(addButton.menu!.children[0...2])
c.append(UIAction(
    title: "Paste Files",
    image: UIImage(systemName: "doc.on.clipboard"),
    attributes: FilesViewController.clipboard.isEmpty ? .disabled : [],
    handler: pasteFiles
))
addButton.menu = addButton.menu!.replacingChildren(c)

Yet this still doesn't do anything. Am I missing some update function I need to call? Or am I not able to modify the menu for a navigation bar button after loading the view?

JackMacWindows
  • 327
  • 3
  • 14

0 Answers0