//
In iOS apps, there are often values and variable sharing in different view and applications, and there are several ways to do this:
1.extern mode
2. Single-case mode
3.delegate mode
The singleton pattern, as the name implies, is only one instance, which ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, you can implement parameter passing between different view
Build a model, inherit and NSObject, and create a singleton
#import <Foundation/Foundation.h>
@interface shareddata: nsobject
#pragma mark---- the way to create a single case
naming habits: share + class name
+ (instancetype) sharedata;
@property (nonatomic, strong)nsstring *inputnssting;
@end
#import "SharedData.h"
@implementation Shareddata
static shareddata *data = nil;
// Singleton class Use this method to create a singleton object
+ (instancetype) sharedata
{
if (data = = Nil) {
// If no object is created, it is yo high sharedata to Create a new object
Data = [[shareddata alloc] init];
}
// if created, returns the object created directly
return data;
}
@end
How to use
This is the assignment of the button binding event for a certain controller, and it is very simple to know which controller to assign the direct value to.
-(void) Button1action: (UIButton *) sender
{
// and then in the case where you need to use a singleton class import this singleton class
[shareddata sharedata]. Inputnssting = self. TextField. Text;
secondviewcontroller *SECONDVC = [[secondviewcontroller alloc] init];< /c9>
[self. Navigationcontroller pushviewcontroller: SECONDVC animated:YES];
}
Single-case pass-through value