Singleton class: A class that is probably understood to have only one object, no matter how it is created, only allocates one memory
These days, need to implement a function, and in a lot of classes to call this method, so I Soching encapsulated a class, directly invoke the method inside the class can be implemented, do not need to write the same method every time (this is the big background), Then in the call I found I needed to use the same object every time (with emphasis), so I needed to encapsulate a singleton class, and it wouldn't waste memory.
Okay, here's the code:
+ (uiview*) Initactivityindictionview
{
static UIView *view = nil;
Static dispatch_once_t predicted;
Dispatch_once (&predicted, ^{
view = [[Self alloc]init];
View.bounds = CGRectMake (0, 0, kscreenwidth, 100);
View.center = Cgpointmake (KSCREENWIDTH/2, 50);
Uiactivityindicatorview *testactivityindicator = [[Uiactivityindicatorview alloc]initwithactivityindicatorstyle: Uiactivityindicatorviewstylegray];
Testactivityindicator.center = Cgpointmake (KSCREENWIDTH/2-60, 50);
Testactivityindicator.bounds = CGRectMake (0, 0, 50, 50);
[Testactivityindicator startanimating];
UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (KSCREENWIDTH/2-30, 25, 120, 50)];
Label.text = @ "trying desperately to load ...";
Label.font = [Uifont systemfontofsize:14];
[View Addsubview:testactivityindicator];
[View Addsubview:label];
});
return view;
}
The above is my own package of a singleton class, test words can create several objects, print (%p) memory can be
Implementation of a singleton class