// 要实现单例传值,那就必须得新建一个类做为单例
+ (instancetype)defaultUIImageView;
// 静态全局变量
static DefaultValueViewController *singleton;
// 提供方法创建单例对象
#pragma mark - defaultUIViewController
+ (instancetype)defaultUIViewController
{
// 懒加载
if (!singleton)
{
singleton = [[DefaultValueViewController alloc] init];
}
return singleton;
}
// 1.堵缺口:避免不是调用你提供的方法创建对象,注意些保证其它方式创建的对象都是同一个
#pragma mark - allocWithZone
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!singleton)
{
singleton = [super allocWithZone:zone];
}
return singleton;
}
// 2.堵缺口:copy方法为对象方法,因此直接返回它的指针就行
#pragma mark - copy
- (id)copy
{
return self;
}
#pragma mark - Xcode
原文:http://www.cnblogs.com/pruple/p/5280539.html