• 单例传值


    /**
     *  单例传值  对象且初始化一次,页面之间相隔很多依旧可传值
     */
    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    #import "AppDelegate.h"
    #import "RootViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
        self.window.rootViewController = navi;
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    #import "RootViewController.h"
    #import "LFViewController.h"
    #import "LFUser.h"//导入单例头文件
    @interface RootViewController ()
    
    @property(nonatomic, strong) UILabel *message ;
    
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //添加按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 150, 60);
        [button setTitle:@"跳到下一个页面" forState:0];
        [button setBackgroundColor:[UIColor greenColor]];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        //添加Label
        self.message = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 60)];
        self.message.backgroundColor = [UIColor greenColor];
        self.message.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:self.message];
        
    }
    /**
     *  页面将要出现时,读取单例的值
     *  如果值为空,不显示;反之,显示
     */
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        //初始化单例
        LFUser *user = [LFUser shareInstance];
        if (user.name.length != 0) {
            //读取单例的值
            NSString *str = [NSString stringWithFormat:@"%@-%@-%@",user.name,user.address,user.hobby];
            self.message.text = str;
        }
    }
    
    /**
     *  按钮事件
     */
    - (void)buttonAction:(UIButton*)sender{
        /**
         *  属性传值,从一个控制器push到下一个控制器使用属性传值比较方便
         */
        LFViewController *lfController = [[LFViewController alloc] init];
        [self.navigationController pushViewController:lfController animated:YES];
    }
    
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface LFViewController : UIViewController
    
    @end
    #import "LFViewController.h"
    #import "LFUser.h"//导入单例头文件
    @interface LFViewController ()
    
    @end
    
    @implementation LFViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //初始化单例
        LFUser *user = [LFUser shareInstance];
        //给单例的属性赋值
        user.name = @"LF";
        user.address = @"中国";
        user.hobby = @"游泳";
        
        //添加按钮
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame = CGRectMake(100, 100, 150, 60);
        [backBtn setTitle:@"返回" forState:0];
        [backBtn setBackgroundColor:[UIColor greenColor]];
        [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backBtn];
    
    }
    /**
     *  backBtn按钮的事件
     */
    - (void)backBtnAction:(UIButton*)sender{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    @end
    #import <Foundation/Foundation.h>
    
    @interface LFUser : NSObject
    
    @property (nonatomic , copy) NSString *name;
    @property (nonatomic , copy) NSString *address;
    @property (nonatomic , copy) NSString *hobby;
    
    + (LFUser*)shareInstance;
    
    @end
    #import "LFUser.h"
    
    static LFUser *instance = nil;
    
    @implementation LFUser
    
    + (LFUser*)shareInstance{
        static dispatch_once_t predicate;
        dispatch_once(&predicate, ^{
            instance = [[self alloc] init];
        });
        return instance;
    }
    
    @end
  • 相关阅读:
    多表关联查询(ORACLE版)
    开发中可能会用到的几个 jQuery 小提示和技巧 (转)
    让jquery easyui datagrid列支持绑定嵌套对象
    java图片处理工具类
    如何为Myeclipse手工添加dtd支持
    二进制与运算
    彻底卸载MYSQL,windows版
    Win7系统安装MySQL5.5.21图解教程
    Rust 资源整理
    【译】我的阅读习惯
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5422089.html
Copyright © 2020-2023  润新知