zoukankan      html  css  js  c++  java
  • 【转】ios -- ViewController跳转+传值(方式一)

    方式一:通过定义一个实体类传值 (从ViewController1 跳转至 ViewController2)

    1、定义实体类NotificationEntity

          .h声明文件

         #import <Foundation/Foundation.h>

        @interface NotificationEntity : NSObject

         {

     

         }

        @property (nonatomic,retainNSString *strTitle; //参数一

        @property (nonatomic,retainNSString *strContent; // 参数二

        @property (nonatomic,retainNSString *strUrl; //参数三

        @end

     

          

         .m实现文件

         #import "NotificationEntity.h"

         @implementation NotificationEntity

         @synthesize strTitle=_strTitle;

         @synthesize strContent=_strContent;

         @synthesize strUrl=_strUrl;

         @end

     

    2、在ViewController2中

          在.h文件中声明实体类NotificationEntity为ViewController2的类成员变量:

     

          @property (retain,nonatomicNotificationEntity *mNotifEntity;

     

          在.m文件中通过@synthesize为成员变量mNotifEntity合成存取方法:

           @synthesize mNotifEntity=_mNotifEntity;

     

    3、在ViewController1中实现跳转并传递参数

                // 组装实体类的实例变量

                NotificationEntity *mNotificationEntity = [[NotificationEntity allocinit];

                [mNotificationEntity setStrTitle:strTitle];

                [mNotificationEntity setStrContent:strContent];

                [mNotificationEntity setStrUrl:strUrl];

                // 实例化ViewController2

                ViewController2 *viewController2 = [[ViewController2 allocinit];

                // 注入参数

                [viewController2 setMNotifEntity:mNotificationEntity];

                // 跳转

                [self.window.rootViewController presentModalViewController:viewController2 animated:YES];

     

    4、在ViewController2中接收参数:

            - (void)viewDidLoad

            {

                [super viewDidLoad];

                [self.lblTitle setText:[self.mNotifEntity strTitle]];

                [self.lblContent setText:[self.mNotifEntity strContent]];

            }

     

    5、在ViewController2中加入返回ViewController1的事件:

            - (IBAction)backOff:(id)sender

            {

                [self dismissModalViewControllerAnimated:YES];

            }

     

    在好多博文中看到@synthesize,下面对这个标志做一下说明:

    1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问

    _age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age的私有成员变量。

    from :http://blog.csdn.net/wanggsx918/article/details/19546987?utm_source=tuicool&utm_medium=referral

  • 相关阅读:
    Neutron分析(5)—— neutron-l3-agent中的iptables
    Neutron分析(4)—— neutron-dhcp-agent
    Neutron分析(3)—— neutron-l3-agent
    Neutron分析(2)——neutron-server启动过程分析
    Neutron分析(1)——简介
    RDO部署openstack(3)
    RDO部署openstack(2)
    Peewee之playhouse中的数据库连接池(原创)
    图数据库简介(转)
    RESTful API 最佳实践(转)
  • 原文地址:https://www.cnblogs.com/xuan52rock/p/5115682.html
Copyright © 2011-2022 走看看