单利方法
- 建工程,指定根视图
- 搭建页面,例如 tabelView,先写死数据
- 准备数据,获取数据
- 存到 model 里(例子: name,age等,设为 model 属性)
- 拿到数据建立 model对象,将信息存入 model 中
通讯界面——单例
// RootViewController.m
[
Singleton
shareSingleton
];
// Singleton.m
+ (
instancetype
) shareSingleton{
// 声明静态变量 , 保证唯一性
// 声明静态变量 , 保证唯一性
//静态变量特点:只初始化一次
static
Singleton
*singleton =
nil
;
if (singleton == nil ) {
singleton = [[ Singleton alloc ] init ];
}
return singleton;
if (singleton == nil ) {
singleton = [[ Singleton alloc ] init ];
}
return singleton;
}
-------------------------------------
// AppDelegate.m
//1
// 得到单例对象
// 得到单例对象
[Singleton shareSingleton].name = @"Ada";
--------------------------------------
// RootViewController.m
- (
void
)viewDidLoad {
[super viewDidLoad];
//2
NSLog(@"name = %@", [Singleton shareSingleton].name);
}
- (
void
)buttonAction:(
UIButton
*)button{
//3
[Singleton
shareSingleton
].
name
=
@
“Nancy
"
;
}
------------------------------------
// FirstViewController.m
//4
NSLog(@"name = %@", [Singleton shareSingleton].name);