系统单例的使用

实现效果: 创建两个页面, 实现两个页面之间的数据通信
下面说的实例练习,是从前往后通信的过程,比较简单, 但是从后往前传值的时候我们需要一个方法来完成, 就是在值传递到第一个页面时, 我们怎样将值更新到控件上, 需要用到"ViewWillAppare"方法来完成.
firstVC.textField———>secondVC.label
实现效果如图:

first.png
<<FirstViewController.m>>文件

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"firstVC";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:(UIBarButtonItemStylePlain) target:self action:@selector(NextAC)] ;

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.label.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.label];


    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 70)];
    self.textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.textField];

}

*//跳转到第二页, 并传值
- (void)NextAC{

    SecondViewController *secondVC = [SecondViewController new];

    //创建系统单例的实例对象
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    //存值
    [userD setObject:self.textField.text forKey:@"FirstText"];

    [self showViewController:secondVC sender:nil];


    //查看单例对象的内存地址(对比两个类中的单例对象是不是同一个对象)
    NSLog(@"%@", userD);
}

@end
----------------------------------------------------------------------------
<<SecondViewController.m>>文件

#import "SecondViewController.h"

@interface SecondViewController ()

@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"secondVC";
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.395 blue:0.584 alpha:1.000];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:(UIBarButtonItemStylePlain) target:self action:@selector(BackAC)] ;


    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    self.label.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.label];


    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 70)];
    self.textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.textField];

    *//创建单例对象来接收数据
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *str = [defaults objectForKey:@"FirstText"];

    //赋值
    self.label.text = str;

}
- (void)BackAC{

    [self.navigationController popViewControllerAnimated:YES];

}

@end```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,163评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,276评论 4 61
  • 1物就是道具 2人类活动的对象,是人的镜子 3电影的细节 4道具的种类 (1)大道具:景的一部分。小道具:手头道具...
    我是戏精阅读 270评论 0 0
  • 清闲的周末就这样过完了,上午还有人陪着,感觉时间过得踏实。下午就孤零零的没事找事,好像又懒得找事,摊在沙发上望着电...
    曉非阅读 327评论 0 0
  • 心慢慢的软弱对这一份工作本不该是这样 什么时候面对所做的所说的是对的是错的 我们是服务人不是忽悠到人是不为难到人 ...
    示人阅读 224评论 0 0