博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单例传值
阅读量:5107 次
发布时间:2019-06-13

本文共 3769 字,大约阅读时间需要 12 分钟。

/** *  单例传值  对象且初始化一次,页面之间相隔很多依旧可传值 */#import 
@interface AppDelegate : UIResponder
@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 
@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 
@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 
@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

 

转载于:https://www.cnblogs.com/lantu1989/p/5422089.html

你可能感兴趣的文章
js抓取list中item的html,html - i want add item in list (vue.js) - Stack Overflow
查看>>
mysql格式化html,MySQL FORMAT()用法及代码示例
查看>>
html倒计时动画,js+css3倒计时动画特效
查看>>
html更改信息显示不允许,html静态页如何加个不允许提交空信息验证?
查看>>
多媒体计算机应用 在哪些方面,多媒体计算机应用于初中英语教学的研究:多媒体计算机教学有哪些特点...
查看>>
html嵌入zabbix网页,通过Zabbix通过Python发送HTML和纯文本电子邮件
查看>>
路由 asp.net mvc html,asp.net-mvc – 为什么需要为Html.Action定义的路由?
查看>>
goquery 查找html标签,goquery库的使用
查看>>
bzoj 3473 后缀自动机多字符串的子串处理方法
查看>>
现金销售与欠款销售
查看>>
MongoDb系列
查看>>
【C语言】控制台窗口图形界面编程(七):鼠标事件
查看>>
BZOJ3732 Network
查看>>
DJango中ORM操作
查看>>
Oracle查询优化--单表查询
查看>>
面试常被问到的小程序问题
查看>>
人类的数学抽象思维
查看>>
时序图
查看>>
beyond compare 4.2.9桌面右键集成的问题修复
查看>>
冒泡排序
查看>>