zoukankan      html  css  js  c++  java
  • 单例传值

    /**
     *  单例传值  对象且初始化一次,页面之间相隔很多依旧可传值
     */
    #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
  • 相关阅读:
    asp.net[web.config] httphandlers 与实现自由定义访问地址
    Web.config配置文件详解
    ASP.NET十分有用的页面间传值方法(转)
    web.config中authorization下的location中的path的设置 (转)
    基于FormsAuthentication的用户、角色身份认证(转)
    FormsAuthentication.RedirectFromLoginPage 登录
    php代码审计-下载站系统Simple Down v5.5.1 xss跨站漏洞分析
    创建二维码生成器
    更新自制入库价格(结账)
    常用的系统存储过程
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5422089.html
Copyright © 2011-2022 走看看