Value passing through an example in IOS
In IOS apps, different views and apps often share values and variables. There are several ways to achieve this:
1. extern Method
2. Singleton Mode
3. delegate Mode
The Singleton mode only has one instance as its name implies. It ensures that a class has only one instance and instantiate it and provides the instance to the entire system. It is often used for application-level shared resource control. This mode is frequently used. You can use a singleton class to transfer parameters between different views.
# Import
@ Interface Session: NSObject @ property (strong, nonatomic) NSString * singleValue; // The singleValue method + (Session *) GetInstance; @ end
# Import "Session. h "@ implementation Session // single instance object static Session * instance; // Single instance + (Session *) GetInstance {@ synchronized (self) {if (instance = nil) {instance = [[self alloc] init] ;}} return instance ;}- (id) init {if (self = [super init]) {self. singleValue = [[NSString alloc] init];} return self ;}@ end
Then, import the singleton class to use the singleton class.
Session * session = [Session GetInstance];
Session. singleValue = @ "Amazing! ";
NSString * value = session. singleValue;