比如我有一个Singleton的类(DemoStatusManage),他有一个实例方法currentStatus会返回一个1-100的随机数。
@interface DemoStatusManage : NSObject
+ (instancetype)sharedManage;
- (int)currentStatus;
@end
@implementation DemoStatusManage {
NSInteger _status;
}
+ (instancetype)sharedManage {
static dispatch_once_t once;
static DemoStatusManage *manage;
dispatch_once(&once, ^{
manage = [[DemoStatusManage alloc] init];
});
return
manage;
}
- (instancetype)init {
self = [
super
init];
if
(self) {
_status = 0;
}
return
self;
}
- (int)currentStatus {
return
[self getRandomNumber:1 to:100];
}
-(int)getRandomNumber:(int)from to:(int)to {
return
(int)(from + (arc4random() % (to - from + 1)));
}
@end
然后在我的另外一个类中会去调用这个Singletion的currentStatus方法,并且将返回的数据渲染到另外那个类的label文案上。
- (void)updateStatusNumber {
self.statusLabel.text = [NSString stringWithFormat:@
"%ld"
,(long)[[DemoStatusManage sharedManage] currentStatus]];
}