iOS 页面间传值 之 属性传值,代理传值
手机 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 页面间传值 之 属性传值,代理传值的更多相关文章
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- 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 ...
随机推荐
- JAVA_Gson_example
package cn.kjxy.GSON; import java.util.List; import cn.kjxy.JSON.HttpHelpers; import com.google.gson ...
- ORACLE 优化
本文主要从大型数据库ORACLE环境四个不同级别的调整分析入手,分析ORACLE的系统结构和工作机理,从九个不同方面较全面地总结了 ORACLE数据库的优化调整方案. 关键词 ORACLE数据库 环境 ...
- WinForm控件小知识
1.DataGridView控件显示自定义表 //造个数据表 DataTable dt = new DataTable(); dt.Columns.Add("DEcode", Sy ...
- 设置ISE/vivado中默认文本编辑器为gvim
ise windows版,添加方式 ISE下点击菜单Edit -> Preferences -> Editor. 在Editor选项框里选择Custom,在Command line syn ...
- TortoiseGit记住用户名和密码
1.将项目从服务器Clone下来 2.然后在Repository上右键,打开设置 3.选择凭证 4.为当前Repository创建一个凭证,然后确定退出即可 5.然后再pull下,会直接提示你输入密码 ...
- Android——列表视图(ListView)
列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项.在android中有两种方法向屏幕中添加列表视图:一种是直接使用ListView组件创建:另外一种是让Activ ...
- ios Trace xcode buile count
前言: 1.记录xcode编辑次数很有必要,特别是在频繁发版本时和根据现有编译次数记录估算工期时间很有帮助 2.全部自动化处理,告别手动时代 正文: 1.新建工程或者现有工程里设置: 然后设置xcod ...
- invalid code signing entitlement的通用暴力解决办法
1.添加的一台苹果设备为开发机子后,打版本,说profile 没找到,报错 2.上传二进制文件到itunes connect ,报错 3.有时候还什么 appID 无效,报错 烦死他了 我的解决办法, ...
- 11_Servlet生命周期
[生命周期] 以前:之前的java程序,我们的Java类自己去new对象,自已实例化对象去调用. 现在:Servlet程序,Servlet的生命周期由TomCat服务器控制的. 我们要研究Servle ...
- C语言 goto, return等跳转
C语言 goto, return等跳转 Please don't fall into the trap of believing that I am terribly dogmatical about ...