NSSavePanel: Squelching the “confirm replace?” dialog

倾然丶 夕夏残阳落幕 提交于 2019-12-05 14:52:09

Here's how it can be done:

  1. Add a delegate to handle NSSavePanel callbacks
  2. Override - (NSString*)panel:(id)sender userEnteredFilename:(NSString*)filename confirmed:(BOOL)okFlag in your delegate
  3. In the delegate:
    1. If okFlag is false, return filename
    2. Otherwise, retain filename as an NSString* in your delegate
    3. Return some unique string that is highly unlikely to be the name of an actual file
  4. When NSSavePanel returns to your code, pull the value of filename from your delegate method, and discard whatever filename NSSavePanel told you (which should be your unique string).

Since userEnteredFilename: is called by the OS before the confirm-replace check is made it gives you a chance to get what the user specified without letting the OS in on the secret. The unique string will assure that the confirm-replace dialog is not popped accidentally.

Gross but efficacious.

No, there is no easy way to do this with NSSavePanel. In theory you could extend NSSavePanel with a category and override certain private methods. I took a quick look though and there is nothing simple about it.

Your customers is going to expect the exact confirmation alert when faced with a NSSavePanel, so don't customize it.

I'm not sure what kind of customized confirm-overwrite dialog you are planning, but might I suggest you use a NSOpenPanel instead, and customize this dialog box with a "Create New File" button? (I believe you can do this via setAccessoryView API.)

For example, if you are asking your customer to choose a file to append new data to, the NSOpenPanel will work quite well; and if the customer want to save the new data to a new file (instead of appending to an existing file), the "Create New File" button is just an additional click.

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