zoukankan      html  css  js  c++  java
  • 关于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属性

  • 相关阅读:
    C#Redis分布式缓存
    CPU核心数
    关于RSA加密
    C#.NET中的CTS、CLS和CLR
    silverlight依赖属性
    silverlight imagesource赋值与转换
    #我的java之多态和接口#
    #什么是 spring boot#
    #替换eclipse自带的maven#
    #从零开始的maven异世界#
  • 原文地址:https://www.cnblogs.com/thxios/p/4828205.html
Copyright © 2011-2022 走看看