iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)
(一)属性传值
(二)代理(委托)传值
代理传值
1.2 创建协议类型的属性 在SecondViewController中创建属性id<postValueDelegate> delegate
1.3 调用属性 即delegate
在SecondViewController页面中 对象传值的方法中调用
[self.delegate postValue:self.textName.text];
1.4 在第一页 即显示修改过信息的页面
遵循协议 实现协议方法 指定代理对象(即 在页面传值参数的方法中为代理赋值)
secondVc.delegate=self;
文件目录:
FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController<UITextFieldDelegate,postValue> @property(strong,nonatomic) UITextField *textName;
@property(strong,nonatomic) UIButton *nextbutton; @end
FirstViewController.m
#import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor redColor];
self.textName=[[UITextField alloc] initWithFrame:CGRectMake(, , ,)];
self.textName.borderStyle=UITextBorderStyleLine;
self.textName.delegate=self;
[self.view addSubview:self.textName]; self.nextbutton=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[self.nextbutton setTitle:@"下一页" forState:UIControlStateNormal];
self.nextbutton.backgroundColor=[UIColor grayColor];
[self.nextbutton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.nextbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
} return YES;
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.textName resignFirstResponder];
} -(void)nextPage
{
SecondViewController *secondVc=[[SecondViewController alloc] init];
secondVc.str=self.textName.text;
secondVc.deletage=self;
[self presentViewController:secondVc animated:YES completion:nil];
} -(void)postValue:(NSString *)str
{
self.textName.text=str;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
SecondViewController.h
#import <UIKit/UIKit.h>
// 创建协议
@protocol postValue <NSObject> -(void)postValue:(NSString *)str; @end @interface SecondViewController : UIViewController<UITextFieldDelegate> @property(strong,nonatomic) UITextField *textInfo;
@property(strong,nonatomic) UIButton *backbutton;
@property(strong,nonatomic) NSString *str; @property(strong,nonatomic) id<postValue> deletage; @end
SecondViewController.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor colorWithRed:0.541 green:0.878 blue:0.831 alpha:1.000]; self.textInfo=[[UITextField alloc] initWithFrame:CGRectMake(, , , )];
self.textInfo.borderStyle=UITextBorderStyleLine;
self.textInfo.text=self.str;
self.textInfo.delegate=self; [self.view addSubview:self.textInfo]; self.backbutton=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[self.backbutton setTitle:@"上一页" forState:UIControlStateNormal];
self.textInfo.text=self.str;
[self.backbutton addTarget:self action:@selector(backPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.backbutton]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
} return YES;
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.textInfo resignFirstResponder];
} -(void)backPage
{
if (self.deletage!=) {
[self.deletage postValue:self.textInfo.text];
} [self dismissViewControllerAnimated:YES completion:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
#import"FirstViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *firstVc=[[FirstViewController alloc] init]; self.window.rootViewController=firstVc; return YES;
}
.......
@end
(三)代码块传值
(四)通知传值
(五)单例传值
iOS--页面间的代理传值(属性、代理(委托)、代码块、单例、通知)的更多相关文章
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- iOS 页面间传值 之 属性传值,代理传值
手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- iOS页面间传值的六种方式
一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传 ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- ios页面间传递参数四种方式
ios页面间传递参数四种方式 1.使用SharedApplication,定义一个变量来传递. 2.使用文件,或者NSUserdefault来传递 3.通过一个单例的class来传递 4.通过Dele ...
随机推荐
- 匹配img和a
a:<\s*a\shref=*([^>]*)>([^<]|<(?!/a))*<\s*/a\s*> img:<img\b[^<>]*?\bsr ...
- 清除linux缓存命令
命令 #sync #echo 3 > /proc/sys/vm/drop_caches 查看内存情况: # more /proc/meminfo Kernels 2.6.16 and newer ...
- 云计算之路-阿里云上:消灭“黑色n秒”第二招——给w3wp进程指定CPU核
虽然昨天的第一招失败了,但是从失败中我们学到了与多核CPU相关的Processor Affinity(处理器关联)的知识. 既然我们可以让.NET程序的不同线程运行于指定的CPU核,那是不是也可以让I ...
- 受限玻尔兹曼机(RBM)学习笔记(八)RBM 的评估
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...
- Screensiz.es – 最流行移动设备及显示器的屏幕规格大全
Screensiz.es 帮助您快速找到目前市场上最流行的设备和显示器的屏幕规格.尺寸数据来自维基百科,使用更好理解的像素密度.流行度推算自 Google 查询(从 AdWords 流量估算),以及一 ...
- suricata学习笔记1--初步认识
1.前言 最近工作需要对网站的关键字进行检测,找出敏感词.这个过程需要对报文进行收集.解码.检测和记录日志.当前只是简单实现功能,根据关键字进行简单的匹配,而没有进行关键字的语义分析.导致的结果就是 ...
- QCustomplot使用分享(六) 坐标轴和网格线
一.概述 前边已经写了5篇对QCustomPlot的讲解,看过上述的几篇文章后,基本就能做一些简单的使用了,但是如果想要做到高度的控制图表,那么坐标轴将是很重要的一部分,因为坐标轴就是图表的一个参考系 ...
- 使用Architecture Explorer分析应用程序及使用层次图
使用Architecture Explorer分析应用程序 Architecture Explorer和依赖图可以帮助我们了解所有的项目,包括小项目和大项目.Architecture Explorer ...
- 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix
Tom and matrix Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...
- MVC WebApi跨域ajax接受post数据笔记
后端api代码示例: [HttpPost] public string callbackUrl([FromBody]SZRCallBackModel cbm) { try { if (cbm == n ...