手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值.

属性传值:属性传值是最简单,也是最常见的一种传值方式,但其具有局限性(一般用于将第一个页面的值传递到第二个页面,但无法从第二个页面传到第一个页面),

  向SecondViewController传值:SecondViewController 设置属性 sendMessage

 - (void)rightButtonAction:(UIBarButtonItem *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.sendMessage = self.rootView.textField.text;
[self.navigationController pushViewController:secondVC animated:YES];
}

代理传值:较难,不易理解,通常用于在第二个页面向第一个页面传值.一般分为六步

(例子采用 navigationController 跳转页面)

1.声明协议 (写在第二个页面)

@protocol myDelegete <NSObject>

- (void)sendMessage:(NSString*)message;

@end

2.定义遵守协议的属性 (写在第二个页面) (属性必须用 assign )

@property (nonatomic , assign)id<myDelegete> delegate;

3.遵守协议(写在第一个页面)

 @interface RootViewController : UIViewController <myDelegete>

4.设置代理 (设置代理写在跳转事件内) (写在第一个页面)

 - (void)rightButtonAction:(UIBarButtonItem *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.sendMessage = self.rootView.textField.text;
[self.navigationController pushViewController:secondVC animated:YES];
//代理传值第四步
secondVC.delegate = self; }

5.实现协议方法 (写在第一个页面)

 - (void)sendMessage:(NSString *)message{
self.rootView.textField.text = message;
}

6.实现传值 (写在第二个页面)

 - (void)leftButtonAction:(UIBarButtonItem *)sender{
[self.navigationController popViewControllerAnimated:YES];
//代理传值第六步:
[self.delegate sendMessage:self.secondView.textField.text];
}

iOS 页面间传值 之 属性传值,代理传值的更多相关文章

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

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

  2. iOS 页面间传值 之 单例传值 , block 传值

    ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...

  3. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

  4. iOS页面间传值的六种方式

    一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传 ...

  5. iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...

  6. iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...

  7. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  8. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  9. ios页面间传递参数四种方式

    ios页面间传递参数四种方式 1.使用SharedApplication,定义一个变量来传递. 2.使用文件,或者NSUserdefault来传递 3.通过一个单例的class来传递 4.通过Dele ...

随机推荐

  1. Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    1.kill掉列出的进程: [root@localhost mysql]# ps -a | grep -i mysql pts/ :: mysqld_safe pts/ :: mysqld <d ...

  2. Flume简介与使用(二)——Thrift Source采集数据

    Flume简介与使用(二)——Thrift Source采集数据 继上一篇安装Flume后,本篇将介绍如何使用Thrift Source采集数据. Thrift是Google开发的用于跨语言RPC通信 ...

  3. CSS Selector (part 1)

    Selector概述 示例: strong { color: red; } 解释: 这是一个完整 css 规则(标签选择器).strong 叫做选择器,它选择该规则将被应用到 DOM 的那个元素上去. ...

  4. flex/bison 计算器

    flex %{ #include <stdio.h> #include "mycalc.tab.h" ;} %} %% "+" return ADD ...

  5. Android之Http网络编程(三)

    在前面两篇博客<Android之Http网络编程(一)>.<Android之Http网络编程(二)>中,简单的介绍了对网页的请求和客户端与服务端的简单的参数交互.那么,这一篇博 ...

  6. 将多维数组转换为支持curl提交的一维数组格式

    /** * @desc 多维数组转化为支持curl提交数组 * @author lytian 2013-06-29 */ public function toPost(array $params = ...

  7. ubuntu中下运行asp.net程序

    首先在ubuntu下面是不能直接运行VISUAL STUTIO的,必须借助mono开发工具和xsp4.0.那我们就来看一下在ubuntu的电脑中怎么安装这两个工具. 首先安装mono,打开终端,输入a ...

  8. 9月18日,SQL学习基础1

    数据库管理和应用 Oltp是小型的管理,OLAP是大型的管理 开发的内容如触发器 数据库管理系统(Database Management System,简称为DBMS)是位于用户与操作系统之间的一层数 ...

  9. bat文件的妙用1-一键开启所有开发软件

    每天早上来的第一件事情,就是打开电脑,然后开一堆的软件 1.wamp 开发环境 2.钉钉   通讯工具 3.PHPstrom 开发工具 4.nodejs.bat Nodejs的扩展(node D:/w ...

  10. linux 配置Socks5

    1.配置 Socks5 编译环境. yum -y install gcc automake autoconf libtool make 2.安装 Socks5 需要的包. yum -y install ...