一:属性传值
传值情景:从前一个页面向后一个页面传值
a.在后一个页面,根据传值类型和个数,写属性
b.在前一个页面, 为属性赋值
c.在后一个页面, 使用值
例如:
第一个视图:
#import "FirstViewController.h"
// 导入头文件
#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;

    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

*************************************************************
第二个视图:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
// 属性传值第一步
@property (nonatomic,copy)NSString *textString;
@end
===============================
#import "SecondViewController.h"
@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];
}
@end

二: 单例传值
传值情景:多个页面间的传值(可从前到后,也可从后向前)
a.创建单例类
b.根据参数的个数和类型, 在单例类中写属性
c.内容存入单例
d.从单例中取内容
缺点:
a.创建对象时, 必须使用单例方法, 不能使用alloc+init
b.单例到程序结束才释放, 内部不能存过大的数据
例子:
注意:(单例传值首先要创建一个单例类)
#import <Foundation/Foundation.h>
// 单例:使用我们自己设计的创建方法,无论在任何地方调用这个方法保证返回的是同一个对象,这个类就叫做单例类
// 注意:单例类不能使用alloc init 创建对象
// 单例的缺点:它所占的堆区的内存永远都不会释放,除非退出程序,所以单例中储存过大的数据
@interface Singleton : NSObject
// 单例传值的第一步:设计单例的创建方法,设计为类方法,方法有返回值,返回值就是在方法内部创建对象
// 单例创建的开头一般都是:share, main, default, stand
+ (Singleton *)shareSingleton;
// 单例传值的第二步:根据要传数据,设置属性
@property (nonatomic,copy)NSString *string;
@end
====================================== 

#import "Singleton.h"
@implementation Singleton
+ (Singleton *)shareSingleton{
    // 创建一个静态区对象指针,保障它的生命周期和程序一样悠长
    // static修饰的变量,初始化方法只走一次
    static Singleton *singleton = nil;
    if (singleton == nil) {
        // 这个对象不能释放
        singleton = [[Singleton alloc]init];
    }
    return singleton;
}
@end
======================================
#import "FirstViewController.h"
#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];
}

@end
*************************************
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
#import "SecondViewController.h"
#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];
}

@end
********************************
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@end
#import "ThirdViewController.h"
#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];

}
@end

三: 代理传值

传值情景: 从后一个页面向前一个页面传值
a.后一个页面(找代理) 制定协议
b.写delegate属性
c.在合适的时候, 让代理执行协议方法
d.前一个页面(成为代理) 建立关系
e.遵守协议
f.实现协议方法
g.通知代理执行协议方法
 例子:
#import "FirstViewController.h"
#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];
}

@end
************************************
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
// 代理传值第一步:制订协议
@optional
// 根据要传的数据类型设计方法的参数
- (void)passValue:(NSString *)string;

@end

@interface SecondViewController : UIViewController

// 代理传值第二步:定义代理协议属性
@property (nonatomic,assign)id <SecondViewControllerDelegate>delegate;

@end

#import "SecondViewController.h"
@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];

}
@end

四: block传值
传值情景: 从后一个页面向前一个页面传值(能写成代理传值, 都可以转化成block传值)
后一个页面
a.对block类型重命名
b.写block属性
c.在合适的时候, 调用block
前一个页面
a.为block赋值
例子:
#import "FirstViewController.h"
#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];

}
@end
*************************************

#import <UIKit/UIKit.h>
// block传值第一步:给block数据类型起别名
typedef void (^BlockName)(NSString *);
@interface SecondViewController : UIViewController
// block传值第二步:定义block属性
@property (nonatomic,copy)BlockName block;
@end
#import "SecondViewController.h"
@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];
}

@end

转载于:https://www.cnblogs.com/Mr-zyh/p/5422858.html

四大传值详解:属性传值,单例传值,代理传值,block传值相关推荐

  1. 详解:设计模式之-单例设计模式

    分享一波:程序员赚外快-必看的巅峰干货 前言 近期预计1-2周左右会更新设计模式专题文章. 单例设计模式:保证在一个JVM中,只能存在一个实例. 应用场景:Servlet,Spring IOC,线程池 ...

  2. 小程序向Java传值,微信小程序 页面传值详解

    微信小程序 页面传值详解 一. 跨页面传值. 1 . 用 navigator标签传值或 wx.navigator, 比如 这里将good_id=16 参数传入detail页面, 然后detail页面的 ...

  3. Android中的四大组件详解

    Android中的四大组件详解 我们都知道Android系统应用层框架中,为开发者提供了四大组件来便于应用的开发,它们是Activity.Service.BroadcastReceiver.Conte ...

  4. Android基础四大组件详解

    Android四大组件详解 博主接触Android开发将近一年,从最初的JavaSE开始,到Android基础,一直学的糊糊涂涂,最近想整理一番 android基础, 顺便把自己的学习开发经验分享给大 ...

  5. Swift - SwiftyJSON的使用详解(附样例,用于JSON数据处理)

    转自:http://www.hangge.com/blog/cache/detail_968.html Swift - SwiftyJSON的使用详解(附样例,用于JSON数据处理) 2016-01- ...

  6. Android笔记——四大组件详解与总结

    android四大组件分别为activity.service.content provider.broadcast receiver. -------------------------------- ...

  7. Matlab v_melcepst函数参数详解(英文附例)

    Matlab v_melcepst函数参数详解(英文附例) 笔者使用的是R2019的matlab,下载了voicebox安装至matlab路径下即可使用.下载voicebox请参看此博客 需要注意的是 ...

  8. android 广播的权限,Android四大组件详解之BroadcastReceiver广播接收者

    Android四大组件详解---BroadcastReceicer广播接收者 广播有两个角色,一个是广播发送者,另一个是广播接收者. 广播按照类型分为两种,一种是全局广播,另一种是本地广播 全局广播: ...

  9. 微信小程序系列4——传值详解

    前言   在开发程序过程中,会遇到各种各样的传值的情景,例如:页面之间的传值.回调.代理.通知等.而在微信小程序中,传值的方式和Android和iOS的方式有一定的异同. 微信小程序使用的数据传值方式 ...

最新文章

  1. 怎么判断网络回路_PLC控制回路故障的判断和检修方法与技巧!
  2. Prebuilt binaries of PCL (point cloud library) for Linux
  3. Python编程语言学习:判断变量是否为NONE或False的几种常见写法(if not用法教程)
  4. htc资料和js和css的嵌套
  5. openfiler的iSCSI配置(二)
  6. sys/queue.h分析(图片复制不过来,查看原文)
  7. [vue-cli] vue-cli中你经常的加载器有哪些?
  8. 环境在c盘_笔记本电脑常见故障--清理C盘空间
  9. TopOn的两种测试方法
  10. linux终端 rmdir,Linux常用命令之rmdir
  11. Rhino学习教程——1.1
  12. mysql应用教程李辉答案_数据库系统原理及mysql应用教程李辉答案章节期末答案...
  13. 高校学生竞赛信息管理系统介绍
  14. 抖音养号教程技巧,做抖音怎么养号上热门
  15. 用人单位不与劳动者签定书面劳动合同的后果
  16. linux计时器命令,安装及使用Linux终端倒数计时器Countdown的方法
  17. 片上总线Wishbone 学习(十)总线周期之单写读操作
  18. [08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is
  19. 全球及中国暗箱针孔相机行业竞争格局及十四五投资规划建议报告2021-2027年版
  20. Linux中几个你不常用,但却很有用的命令

热门文章

  1. 分享Silverlight/WPF/Windows Phone一周学习导读(07月25日-07月31日)
  2. linux挂载硬盘简书,linux|挂载硬盘及分区
  3. CIC抽取插值滤波器和RCF
  4. 递归式求时间复杂度的代入法与迭代法的举例讲解
  5. 难点电路详解之负反馈放大器电路(4)
  6. vue悬停改变背景颜色
  7. 解决无线网卡 RTL8723BE ubuntu环境下不稳定情况
  8. 利用async和await异步操作解决node.js里面fs模块异步读写,同步结果的问题
  9. C语言程序设计第一节课作业
  10. 我是IT小小鸟 读书笔记