四大传值详解:属性传值,单例传值,代理传值,block传值
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
// 配置导航栏标题
self.navigationItem.title = @"FirstVC";
// 创建导航栏右侧按钮
[self customRightItem];
// 创建textField
[self customTextField];
}
#pragma mark - 创建textField控件方法
- (void)customTextField{
// 创建控件
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
[_textField release];
}
#pragma mark - 创建导航栏右侧按钮
- (void)customRightItem{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStyleDone target:self action:@selector(handleRight:)];
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];
}
#pragma mark - 导航栏右侧按钮关联方法
- (void)handleRight:(UIBarButtonItem *)sender{
// 创建跳转到的页面视图
SecondViewController *secondVC = [[SecondViewController alloc]init];
// 属性传值第二步
// 在push之前将_textField的值取出来赋值给secondVC.textString
// secondVC.textString = _textField.text;
[secondVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@interface SecondViewController : UIViewController
// 属性传值第一步
@property (nonatomic,copy)NSString *textString;
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)dealloc
{
self.textString = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
// 配置导航栏标题
self.navigationItem.title = @"SecondVC";
// 创建label
[self customLabel];
// 属性传值第三步
// 将属性中存储的值取出来赋值给_label.text
_label.text = _textString;
}
#pragma mark - 创建label方法
- (void)customLabel{
// 创建label对象
self.label = [[UILabel alloc]initWithFrame:CGRectMake(100,100,120,40)];
_label.layer.cornerRadius = 7;
_label.backgroundColor = [UIColor cyanColor];// label默认透明,需要赋颜色
[self.view addSubview:_label];
[_label release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// 单例:使用我们自己设计的创建方法,无论在任何地方调用这个方法保证返回的是同一个对象,这个类就叫做单例类
// 注意:单例类不能使用alloc init 创建对象
// 单例的缺点:它所占的堆区的内存永远都不会释放,除非退出程序,所以单例中储存过大的数据
@interface Singleton : NSObject
// 单例传值的第一步:设计单例的创建方法,设计为类方法,方法有返回值,返回值就是在方法内部创建对象
// 单例创建的开头一般都是:share, main, default, stand
+ (Singleton *)shareSingleton;
// 单例传值的第二步:根据要传数据,设置属性
@property (nonatomic,copy)NSString *string;
@implementation Singleton
+ (Singleton *)shareSingleton{
// 创建一个静态区对象指针,保障它的生命周期和程序一样悠长
// static修饰的变量,初始化方法只走一次
static Singleton *singleton = nil;
if (singleton == nil) {
// 这个对象不能释放
singleton = [[Singleton alloc]init];
}
return singleton;
}
#import "Singleton.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)dealloc
{
self.textField = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
// 配置导航栏标题
self.navigationItem.title = @"FirstVC";
// 创建导航栏右侧按钮
[self customRightItem];
// 创建textField
[self customTextField];
}
#pragma mark - 创建textField控件方法
- (void)customTextField{
// 创建控件
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
[_textField release];
}
#pragma mark - 创建导航栏右侧按钮
- (void)customRightItem{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStyleDone target:self action:@selector(handleRight:)];
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];
}
#pragma mark - 导航栏右侧按钮关联方法
- (void)handleRight:(UIBarButtonItem *)sender{
// 创建跳转到的页面视图
SecondViewController *secondVC = [[SecondViewController alloc]init];
// 单例传值第三步
[Singleton shareSingleton].string = _textField.text;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@interface SecondViewController : UIViewController
#import "Singleton.h"
@interface SecondViewController ()
@property (nonatomic,retain)UILabel *label;
@end
@implementation SecondViewController
- (void)dealloc
{
self.textString = nil;
self.label = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
// 配置导航栏标题
self.navigationItem.title = @"SecondVC";
// 创建导航栏左侧按钮
[self customLeftItem];
// 创建导航栏右侧按钮
[self customRightItem];
// 创建label
[self customLabel];
// 单例传值第四部
_label.text = [Singleton shareSingleton].string;
}
#pragma mark - 创建label方法
- (void)customLabel{
// 创建label对象
self.label = [[UILabel alloc]initWithFrame:CGRectMake(100,100,120,40)];
_label.layer.cornerRadius = 7;
_label.backgroundColor = [UIColor cyanColor];// label默认透明,需要赋颜色
[self.view addSubview:_label];
[_label release];
}
#pragma mark - 创建导航栏左侧按钮方法
- (void)customLeftItem{
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(handleLeft:)];
self.navigationItem.leftBarButtonItem = leftItem;
[leftItem release];
}
#pragma mark - 导航栏左侧点击关联方法
- (void)handleLeft:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建导航栏右侧按钮方法
- (void)customRightItem{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStyleDone target:self action:@selector(handleRightItem:)];
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];
}
#pragma mark - 导航栏右侧跳转方法
- (void)handleRightItem:(UIBarButtonItem *)sender{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@interface ThirdViewController : UIViewController
#import "Singleton.h"
@interface ThirdViewController ()
@property (nonatomic,retain)UILabel *label;
@end
@implementation ThirdViewController
- (void)dealloc
{
self.label = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
self.navigationItem.title = @"ThinrdVC";
[self customLabel];
// 单例传值
_label.text = [Singleton shareSingleton].string;
}
#pragma mark - 创建label
- (void)customLabel{
self.label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_label.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_label];
[_label release];
三: 代理传值
#import "SecondViewController.h"
// 代理传值第四步:代理对象所在的类遵循协议
@interface FirstViewController ()<SecondViewControllerDelegate>
@property (nonatomic,retain)UILabel *label;
@end
@implementation FirstViewController
- (void)dealloc
{
self.label = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"FirstVC";
// 创建label
[self customLabel];
// 创建Button
[self customButton];
}
#pragma mark - 创建label
- (void)customLabel{
self.label = [[UILabel alloc]initWithFrame:CGRectMake(50,100, 220, 40)];
_label.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_label];
[_label release];
}
#pragma mark - 创建Button
- (void)customButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(130, 180, 50, 50);
[button setTitle:@"下一级" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleButton:(UIButton *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
// 代理传值第三步:指定代理对象
secondVC.delegate = self;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
// 代理传值第五步:实现协议方法
- (void)passValue:(NSString *)string{
_label.text = string;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@protocol SecondViewControllerDelegate <NSObject>
// 代理传值第一步:制订协议
@optional
// 根据要传的数据类型设计方法的参数
- (void)passValue:(NSString *)string;
@end
@interface SecondViewController : UIViewController
// 代理传值第二步:定义代理协议属性
@property (nonatomic,assign)id <SecondViewControllerDelegate>delegate;
@interface SecondViewController ()
@property (nonatomic,retain)UITextField *textField;
@end
@implementation SecondViewController
- (void)dealloc
{
self.textField = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.title = @"SecondVC";
// 创建texField
[self customTextField];
// 创建导航左边按钮
[self customLeft];
}
#pragma mark - 创建texField
- (void)customTextField{
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 220, 40)];
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
[_textField release];
}
#pragma mark - 创建导航左边按钮
- (void)customLeft{
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回上一级" style:UIBarButtonItemStyleDone target:self action:@selector(handleLftItem:)];
self.navigationItem.leftBarButtonItem = leftItem;
[leftItem release];
}
- (void)handleLftItem:(UIBarButtonItem *)sender{
// 代理传值第六步:在pop之前通知代理对象执行协议中的方法
// 判断代理对象有没有实现协议中的方法
if ([_delegate respondsToSelector:@selector(passValue:)]) {
[_delegate passValue:_textField.text];
}
[self.navigationController popViewControllerAnimated:YES];
#import "SecondViewController.h"
@interface FirstViewController ()
@property (nonatomic,retain)UILabel *label;
@end
@implementation FirstViewController
- (void)dealloc
{
self.label = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
self.navigationItem.title = @"FirstVC";
// 创建label
[self customLabel];
// 创建Button
[self customButton];
}
#pragma mark - 创建label
- (void)customLabel{
self.label = [[UILabel alloc]initWithFrame:CGRectMake(50,100, 220, 40)];
_label.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_label];
[_label release];
}
#pragma mark - 创建Button
- (void)customButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(130, 180, 50, 50);
[button setTitle:@"下一级" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleButton:(UIButton *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
// block传值第三步:给block属性赋值,存储一段代码块,在合适的时机回调block内所传的值
secondVC.block = ^(NSString *string){
_label.text = string;
};
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
// block传值第一步:给block数据类型起别名
typedef void (^BlockName)(NSString *);
@interface SecondViewController : UIViewController
// block传值第二步:定义block属性
@property (nonatomic,copy)BlockName block;
@interface SecondViewController ()
@property (nonatomic,retain)UITextField *textField;
@end
@implementation SecondViewController
- (void)dealloc
{
self.textField = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.title = @"SecondVC";
// 创建texField
[self customTextField];
// 创建导航左边按钮
[self customLeft];
}
#pragma mark - 创建texField
- (void)customTextField{
self.textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 220, 40)];
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
[_textField release];
}
#pragma mark - 创建导航左边按钮
- (void)customLeft{
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回上一级" style:UIBarButtonItemStyleDone target:self action:@selector(handleLftItem:)];
self.navigationItem.leftBarButtonItem = leftItem;
[leftItem release];
}
- (void)handleLftItem:(UIBarButtonItem *)sender{
// block传值第四步:block属性的调用(类似于代理传值中的通知代理对象实现协议方法过程)
_block(_textField.text);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
四大传值详解:属性传值,单例传值,代理传值,block传值的更多相关文章
- Android笔记——四大组件详解与总结
android四大组件分别为activity.service.content provider.broadcast receiver. ------------------------------- ...
- [ 单例、代理 & 通知 ]
PS:手写单例.代理方法实现 & 通知的简单使用! [ 单例模式,代理设计模式,观察者模式! ] 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设 ...
- spring-搭建-概念-配置详解-属性注入
1 spring介绍 三层架构中spring位置 spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还能帮其 ...
- 分布式事务(Seata) 四大模式详解
前言 在上一节中我们讲解了,关于分布式事务和seata的基本介绍和使用,感兴趣的小伙伴可以回顾一下<别再说你不知道分布式事务了!> 最后小农也说了,下期会带给大家关于Seata中关于sea ...
- 详解Bootstrap表单组件
表单常见的元素主要包括:文本输入框.下拉选择框.单选框.复选框.文本域.按钮等.下面是不同的bootstrap版本: LESS: forms.less SASS: _forms.scss boot ...
- android 四大组件详解
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity详解二.Service详解三.Broadcast Receiver详解四.Content Provider详解外加一个重 ...
- spring学习 十七 scope属性,单例多例
Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...
- Java四大引用详解:强引用、软引用、弱引用、虚引用
面试官考察Java引用会问到强引用.弱引用.软引用.虚引用,具体有什么区别?本篇单独来详解 @mikechen Java引用 从JDK 1.2版本开始,对象的引用被划分为4种级别,从而使程序能更加灵活 ...
- iOS常用设计模式:MVC、单例、代理、观察者。
MVC 模型-视图-控制器(MVC)设计模式 MVC根据角色划分类,涉及到三个角色: Model:模型保存应用程序的数据. View:视图是模型的可视化表示以及用户交互的控件. Controller: ...
随机推荐
- NHibernate学习(零)-本次学习遇到的错误汇总
问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...
- [ HAOI 2010 ] 最长公共子序列
\(\\\) \(Description\) 求两个长度\(\le5000\)的大写字母串的\(LCS\)长度及个数,定义两\(LCS\)中某一字符在两序列出现位置有一处不同就视为不同. \(\\\) ...
- Oracle Sequence不设置cache参数的几个潜在问题(转载)
转载于 http://www.uml.org.cn/sjjm/201204065.asp 在Oracle中,我们没有MYSQL和SQL Server ...
- TensorFlow: Could not load requested Qt binding.
使用Eclipse 引入tensorflow,出现 Could not load requested Qt binding. 问题 ImportError: Could not load reque ...
- LVS部分调度算法的适应场景分析
1.轮叫调度算法(RR)假设所有服务器处理性能均相同,不管服务器的当前连接数和响应速度.该算法相对简单,不适用于服务器组中处理性能不一的情况,而且当请求服务时间变化比较大时,轮叫调度算法容易导致服务器 ...
- MYSQL数据库迁移到ORACLE数据库
一.环境和需求1.环境 MySQL数据库服务器: OS version:Linux 5.3 for 64 bit mysql Server version: 5.0.45 Oracle数据库服务器: ...
- gitlab 第1次提交代码到1个新仓库
1.如果是本地刚刚搭建好git环境,第一次和gitlab服务器产生连接 参照这个文 https://www.cnblogs.com/kaerxifa/p/10929098.html 2.已经和gitl ...
- CentOS7阿里云服务器,python程序requests无法正常post网站(报502)
问题描述: 使用jenkins构建接口自动化测试时,发现新增加的接口case不能访问通,会报502错误(本地可以跑通,在测试服就会502)解决的思路: 缩小调试范围(去掉jenkins db环境,将问 ...
- linux安装mysql可视化工具MySQL-workbench 连接数据库 执行sql
Step1:建立数据库连接 点击新建连接的按钮,符号是“+”的按钮,出现下图,在“Connection name”输入连接名称. 填写连接信息 输入数据库连接密码 测试连接: 再次点击连接时会要求输入 ...
- 洛谷——P3205 [HNOI2010]合唱队
P3205 [HNOI2010]合唱队 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为 ...