首页 技术 正文
技术 2022年11月13日
0 收藏 541 点赞 2,334 浏览 1319 个字

手机 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];
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844