How do I ask the user for a file name?

冷暖自知 提交于 2019-12-05 08:31:10

The easiest way is to use:

UIManager default chooseFileMatching: nil

You can specify patterns as:

UIManager default chooseFileMatching: #('*.jpg' '*.png')

You can also specify a label for the dialog:

UIManager default
    chooseFileMatching: #('*.jpg' '*.png')
    label: 'Please select and image to process'

What I use in one of my demo applications is

MindmapNode class>>open
    |fileName|
    fileName := UITheme current chooseFileIn: World title: 'Choose file' extensions: nil path: nil preview:nil.
    fileName ifNotNil: [ (FLMaterializer materializationFromFileNamed: fileName)
        root openInWorld attachAllSubnodes detachAllSubnodes ]

MindmapNode>>saveMap
    |fileName|
    fileName := UITheme current fileSaveIn: World title: 'Choose file' extensions: nil path: nil.
    fileName ifNotNil: [ FLSerializer serialize: self toFileNamed: fileName].

The UIManager cares about being able to run headless, from a command line. Then you want to be able to provide a file name in either a parameter or an input file. In the standard in-image situation, UIManager default is a MorphicUIManager that delegates to the current theme.

So using the UIManager would probably be better

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!