OS-X: I want to pass the value of the variable from ViewController to AppDelegate.

Asked 2 years ago, Updated 2 years ago, 38 views

I want to create an OS-X application with swift, but I don't know how to pass the value from ViewController to AppDelegate.

I would like to receive a value in AppDelegate applicationWillTermination (aNotification:NSNotification).

I would like to declare this class variable in AppDelegate Class in the form of var Flag:Int?, but I don't know how to have ViewController set the value.

Thank you for your cooperation.

swift macos

2022-09-30 15:33

1 Answers

For iOS, UIApplication.sharedApplication().delegate allows you to see an instance of AppDelegate, which is the same for OS X.

NSApplication.sharedApplication().delegate as?AppDelegate

It is now available.

I'd like to use this class variable by declaring it in the form of var Flag:Int? in the AppDelegate Class.

Swift, like Objective-C, had the instance variable but no class variable...
One technique of Swift is to keep variables as optional as possible.
Following Swift's naming convention, variables start with lowercase letters.

 var flag: Int=0

If you give an initial value, you can make it non-Optionsal and use it without ! or ?.

Gift:

AppDelegate is in the Responder Chain path, so Action with the target set to nil can be received by Action methods written in the AppDelegate class if no Action method is written in the middle of the path.


2022-09-30 15:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.