1.属性传值和代理传值

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate,postValuedelegate>@property (nonatomic ,strong) UITextField *textfld;
@property (nonatomic ,strong) UIButton *button;
@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//设置背景色self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"18103807_114944308127_2.jpg"]];self.textfld = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];self.textfld.backgroundColor = [UIColor grayColor];self.textfld.textColor = [UIColor blueColor];//指定隐藏键盘的代理self.textfld.delegate = self;[self.view addSubview:self.textfld];//设置跳到下一页的按钮self.button = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 40)];[self.button setTitle:@"下一页" forState:UIControlStateNormal];[self.button addTarget:self action:@selector(nextPage) forControlEvents: UIControlEventTouchUpInside ];[self.view addSubview:self.button];
}//隐藏键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{if ([self.textfld isFirstResponder]) {[self.textfld resignFirstResponder];}return YES;
}
-(void)nextPage
{SecondViewController *second = [[SecondViewController alloc] init];//把本页的值赋给下一页的属性,实现传值second.str = self.textfld.text;[self presentViewController:second animated:YES completion:^{NSLog(@"跳转成功");}];second.delegate = self;
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}//实现代理方法
-(void)postValue:(NSString *)info{self.textfld.text = info;}@end
//第二页代码
#import <UIKit/UIKit.h>
//申明一个传值协议
@protocol postValuedelegate <NSObject>
//传值协议的方法
-(void)postValue:(NSString *) info;@end@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) NSString *str;
@property (nonatomic ,strong)  UITextField *textFil;
@property (nonatomic ,assign) id<postValuedelegate> delegate;@end
#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];self.textFil = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];self.textFil.backgroundColor = [UIColor grayColor];self.textFil.textColor = [UIColor greenColor];//属性传值self.textFil.text = self.str;self.textFil.delegate = self;[self.view addSubview:self.textFil];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}-(BOOL)textFieldShouldReturn:(UITextField *)textField{if (self.delegate) {[self.delegate postValue:self.textFil.text];}if ([self.textFil isFirstResponder]) {[self.textFil resignFirstResponder];}[self dismissViewControllerAnimated:YES completion:nil];return YES;
}@end

2.代码块传值

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) UITextField *textName;
@property (nonatomic ,strong) UIButton *button;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];self.textName = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];self.textName.backgroundColor = [UIColor grayColor];self.textName.textColor = [UIColor greenColor];self.textName.delegate = self;[self.view addSubview:self.textName];self.button = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 40)];[self.button setTitle:@"下一页" forState:UIControlStateNormal];[self.button addTarget:self action:@selector(nextPage) forControlEvents: UIControlEventTouchUpInside ];[self.view addSubview:self.button];}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{if ([self.textName isFirstResponder]) {[self.textName resignFirstResponder];}return YES;
}-(void)nextPage
{SecondViewController *second =  [[SecondViewController alloc] init];second.str = self.textName.text;second.postvalueb = ^(NSString *str){self.textName.text = str;NSLog(@"%@",str);};[self presentViewController:second animated:YES completion:^{NSLog(@"页面跳转成功");}];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
#import <UIKit/UIKit.h>
typedef void(^postValueBlock)(NSString *);@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic ,strong) NSString *str;
@property (nonatomic ,strong) postValueBlock postvalueb;
@property (nonatomic ,strong)  UITextField *textFil;
@end
#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"5622630220130817130527093.jpg"]];self.textFil = [[UITextField alloc ] initWithFrame:CGRectMake(100, 100, 100, 50)];self.textFil.backgroundColor = [UIColor grayColor];self.textFil.textColor = [UIColor greenColor];//属性传值self.textFil.text = self.str;self.textFil.delegate = self;[self.view addSubview:self.textFil];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}-(BOOL)textFieldShouldReturn:(UITextField *)textField
{if(self.postvalueb){self.postvalueb(self.textFil.text);}if ([self.textFil isFirstResponder]) {[self.textFil resignFirstResponder];}[self dismissViewControllerAnimated:YES completion:nil];return YES;
}@end

3.单列传值

#import <UIKit/UIKit.h>
//创建一个单列类
@interface MyPickerView : UIPickerView<NSCopying>
+(MyPickerView *)shareInstance;
@end
//创建一个单列类
#import "MyPickerView.h"
static MyPickerView *singleton;
@implementation MyPickerView
+(MyPickerView *)shareInstance
{    if(singleton == nil){singleton = [[MyPickerView alloc] init];}return singleton;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{if (singleton == nil) {singleton = [super allocWithZone:zone];}return singleton;
}
-(id)copyWithZone:(NSZone *)zone
{return self;
}@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "SecondViewController.h"
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic ,strong) NSArray *array;
@property (nonatomic ,strong) MyPickerView *mypickerV;
@property (nonatomic ,strong)  UIButton *button;@end#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor brownColor];self.array = @[@"aa",@"bb",@"cc",@"dd"];self.mypickerV = [[MyPickerView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];self.mypickerV.delegate = self;self.mypickerV.dataSource = self;self.mypickerV.backgroundColor = [UIColor grayColor];[self.view addSubview:self.mypickerV];self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];self.button.backgroundColor = [UIColor greenColor];[self.button setTitle:@"下一页" forState: UIControlStateNormal];[self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:self.button];}-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{return self.array.count;
}-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return self.array[row];}-(void)nextPage
{SecondViewController *second = [[SecondViewController alloc] init];[self presentViewController:second animated:YES completion:nil];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@property (nonatomic ,strong) MyPickerView  *mypick;
@property (nonatomic ,strong)  UIButton *button;
@end#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor blueColor];self.mypick = [[MyPickerView alloc] init];[self.view addSubview:self.mypick];self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];self.button.backgroundColor = [UIColor greenColor];[self.button setTitle:@"下一页" forState: UIControlStateNormal];[self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:self.button];self. mypick.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"u=113715476,712357466&fm=21&gp=0.jpg"]];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}-(void)nextPage
{ThirdViewController *third = [[ThirdViewController alloc] init];[self viewDidAppear:YES];[self presentViewController:third animated:YES completion:nil];}@end
#import <UIKit/UIKit.h>
#import "MyPickerView.h"
#import "ViewController.h"
@interface ThirdViewController : UIViewController
@property (nonatomic ,strong) MyPickerView *mypick;
@property (nonatomic ,strong)  UIButton *button;
@end
#import "ThirdViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad {[super viewDidLoad];self.mypick = [[MyPickerView alloc] init];[self.view addSubview:self.mypick];self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 310, 100, 40)];self.button.backgroundColor = [UIColor greenColor];[self.button setTitle:@"下一页" forState: UIControlStateNormal];[self.button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:self.button];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}-(void)nextPage
{ViewController *second = [[ViewController alloc] init];[self presentViewController:second animated:YES completion:nil];}
-(void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated];}@end

4.通知传值

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{NSDictionary *dic = @{@"name":@"liumu",@"age":@"22"};NSNotification *notification = [[NSNotification alloc] initWithName:@"show" object:nil userInfo:dic];// 取value值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"show" object:notification];//发送消息取键值// [[NSNotificationCenter defaultCenter] postNotification:notification];
    SecondViewController *second = [[SecondViewController alloc] init];[self presentViewController:second animated:YES completion:nil];}
@end
#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController
@property (nonatomic ,strong) UITextField *TextFil;
@end#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor purpleColor];self.TextFil.backgroundColor = [UIColor blueColor];self.TextFil = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];self.TextFil.textColor = [UIColor greenColor];[self.view addSubview:self.TextFil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Show:) name:@"show" object:nil];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}//传值
-(void)Show:(NSNotification *)notification
{NSString *str=(NSString *)[notification object];//self.TextFil.text = str;NSLog(@"%@",str);
}//注册完毕,销毁通知
-(void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self name:@"show" object:nil];
}@end

转载于:https://www.cnblogs.com/liumu/p/5343076.html

iOS 五种传值方式相关推荐

  1. android 之Activity的五种传值方式 (在从当前Activity跳转到目标Activity时的传值方式)

    A.使用intent(意图)传值 MainActivity(源文件):         1.intent.putExtra(key,value):key一般是String,值为 java八大基本数据类 ...

  2. iOS 页面间几种传值方式(属性,代理,block,单例,通知)

    第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...

  3. iOS五种本地缓存数据方式

    iOS五种本地缓存数据方式 iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据全部 ...

  4. JavaScript中this的五种绑定方式详解

    1 this的五种绑定方式 1.1 默认绑定 默认绑定是指当函数调用时,没有为其指定对象上下文,此时会将该函数的this绑定到全局对象(window对象).自ES5有了严格模式之后,默认绑定方式又分为 ...

  5. 五种排序方式gif展示【python】

    简述 有五种排序方式. 文章目录 简述 排序 简单排序 冒泡排序 选择排序 归并排序 快速排序 排序 简单排序 import numpy as np import matplotlib.pyplot ...

  6. Silverlight C# 游戏开发:游戏循环体的五种设计方式

    我们在游戏设计和开发中,尤其是引擎开发中,逻辑循环是一个重要组成部分,循环决定了游戏的基础逻辑和运行方式,在不同的开发环境和语言下,对于循环的释义甚至相差甚远,那么我想和大家分享的是在Silverli ...

  7. 谈表达式树的缓存(7):五种缓存方式的总体分析及改进方案

    终于到了这个系列的最后一篇文章了,这个系列的文章本是许多话题的基础,却拖了那么长时间还没有完结.这篇文章主要讨论五种缓存方式各自的优劣,以及他们的性能关键在什么地方,如果要进行改进又有什么可选方案.在 ...

  8. Java防止Xss注入json_XSS的两种攻击方式及五种防御方式

    XSS介绍 跨站脚本攻击指的是自己的网站运行了别的网站里面的代码 攻击原理是原本需要接受数据但是一段脚本放置在了数据中: 该攻击方式能做什么? 获取页面数据 获取Cookies 劫持前端逻辑 发送请求 ...

  9. 后端技术:Java定时任务的五种创建方式

    Quartz表达式生成地址:http://cron.qqe2.com/ 支持生成定时任务表达式和反解析,使用Quartz表达式的定时任务如下 xxl-job springboot 的 @Schedul ...

最新文章

  1. 客快物流大数据项目(五十三):实时ETL模块开发准备
  2. 资本主义社会是不存在人道的
  3. 如何用jsp在线自动批改_推荐:5个好用的免费自动化在线营销工具
  4. 【ios开发】图片拉伸
  5. ES6的变量声明详述
  6. 会php不回缓存行吗?多重实现
  7. 如何使用SQL Server链接服务器查询Excel数据
  8. Samba 3.4.0 发布
  9. android:异步任务asyncTask介绍及异步任务下载图片(带进度条)
  10. linux内存和缓冲区,Linux上怎么清除缓存、缓冲区和交换区空间?
  11. Vue.js 第5章 webpack配置
  12. vue 点击图标 显示/隐藏 密码
  13. 周报,当前是第几周 ?
  14. 深度分析短视频搬运视频消重九种方法各平台轻松过原创
  15. 浅谈机器人控制与仿真设计----RDS和ROS
  16. Word中endnote选项无效,怎么处理?
  17. 【算法笔记】极客时间 算法面试通关40讲 笔记  覃超
  18. 创建BUG时增加字段
  19. 老慜的A5作业——p5.js 动态、周期、随机、面向对象
  20. burpsuite激活

热门文章

  1. solving order
  2. 蛮力法 —— 求解最大连续子序列和问题
  3. 设计学生证信息管理系统(C++实现,附源代码,详细解析)
  4. OpenGL基础32:面剔除
  5. OpenGL基础31:混合
  6. Codeforces Round #223 (Div. 2): C. Sereja and Prefixes(二分+递归)
  7. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会(Trajan)
  8. pytorch 中 torch.cat 函数的使用
  9. Perceptual Losses for Real-Time Style Transfer and Super-Resolution 运行程序
  10. AD16查看不同层并修改走线(一般也就两层