Matt
Matt

Reputation: 2722

How to link actions to File->Save/File->Open in Swift app

I have a Swift application that has data I want to save using the methods described in this question. Now, I need to know what is the proper way to link these actions to the File -> Save/Save As menu item and the File -> Open menu item. This isn't a document-based application.

I'm running Xcode 6.4 on OS X 10.10.4.

Upvotes: 0

Views: 1073

Answers (2)

SammyRNYCreal
SammyRNYCreal

Reputation: 781

In Swift 3, it may seem odd because it's using 'First Responder' but all you have to do is add the following code to your NSViewController class that is set as the Custom Class on a storyboard. It does not have to be connected like other @IBAction functions.

class Test: NSViewController {

@IBAction func saveDocument(_ sender: Any?) {
// code to execute for save functionality
// following line prints in debug to show function is executing.
// delete print line below when testing is completed.
Print("save")  
}

@IBAction func openDocument(_ sender: Any?) {
// code to execute for open functionality here
// following line prints in debug to show function is executing.
// delete print line below when testing is completed.
 print("open")
}

}

Upvotes: 0

Cai
Cai

Reputation: 3659

Create an IBAction function and link it to the XIB via Interface Builder.

Create an open/save panel in that function and let the user select the file name and location, use the returned NSURL array for saving/loading path. (after converted to required object type, of course.)

There are lots of example codes almost everywhere, either Objective-C or Swift.

Upvotes: 1

Related Questions