关于UIApplication单例传值

由于UIApplication的是一个系统级别的单例,那么就能够省去自己创建单例的方法,将需要需要的类对象,在UIApplication单例内声明一个,通过点语法来实现单个

 

需要调用的实现单例模式的类为User类

 

在AppDelegate.h内声明一个User对象,通过UIApplication 来获得该对象

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 #import "User.h"
3 
4 @interface AppDelegate : UIResponder <UIApplicationDelegate>
5 
6 @property (strong, nonatomic) UIWindow *window;
7 @property (strong, nonatomic)User *currentUser;
8 
9 @end

 

AppDelegate.m文件内不需要增加任何的方法与属性

 

 

currentUser的get与set

User * current = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).currentUser;

 

((AppDelegate*)[[UIApplication sharedApplication] delegate]).currentUser=[[User alloc]init];

 

点语法前为获取UIApplication 单例对象,.currentUser为获取AppDelegate对象的currentUser属性

 

posted on 2015-09-22 10:28  坚持才能不懈  阅读(160)  评论(0编辑  收藏  举报

导航