代码改变世界

单例传值

2016-04-22 17:15  甘雨路  阅读(231)  评论(0编辑  收藏  举报
/**
 *  单例传值  对象且初始化一次,页面之间相隔很多依旧可传值
 */
#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