zoukankan      html  css  js  c++  java
  • iOS 页面间传值 之 属性传值,代理传值

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

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

      向SecondViewController传值:SecondViewController 设置属性 sendMessage

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

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

    (例子采用 navigationController 跳转页面)

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

    @protocol myDelegete <NSObject>
    
    - (void)sendMessage:(NSString*)message;
    
    @end

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

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

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

    1 @interface RootViewController : UIViewController <myDelegete>

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

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

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

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

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

    1 - (void)leftButtonAction:(UIBarButtonItem *)sender{
    2     [self.navigationController popViewControllerAnimated:YES];
    3     //代理传值第六步:
    4     [self.delegate sendMessage:self.secondView.textField.text];
    5 }
  • 相关阅读:
    Nginx源码编译安装与负载均衡配置实践
    jquery_file_upload in Rails(ajax实现多张图片上传)
    Nokogiri相关网站
    rails中 url和path的区别
    Unique Paths
    Kinect for windows的脸部识别
    .Net给图片添加水印效果
    大型高性能ASP.NET系统架构设计
    如何来提高网站的访问速度,其中包括服务器的配置建议,asp.NET代码的编写及前端页面的优化等等
    URL中#号(井号)的作用
  • 原文地址:https://www.cnblogs.com/Ager/p/4907955.html
Copyright © 2011-2022 走看看