ios -- ViewController跳转+传值(方式一)

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

1、定义实体类NotificationEntity

      .h声明文件

     #import <Foundation/Foundation.h>

    @interface NotificationEntity : NSObject

     {


     }

    @property (nonatomic,retain) NSString *strTitle; //参数一

    @property (nonatomic,retain) NSString *strContent; // 参数二

    @property (nonatomic,retain) NSString *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,nonatomic) NotificationEntity *mNotifEntity;


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

       @synthesize mNotifEntity=_mNotifEntity;


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

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

            NotificationEntity *mNotificationEntity = [[NotificationEntity alloc] init];

            [mNotificationEntity setStrTitle:strTitle];

            [mNotificationEntity setStrContent:strContent];

            [mNotificationEntity setStrUrl:strUrl];

            // 实例化ViewController2

            ViewController2 *viewController2 = [[ViewController2 alloc] init];

            // 注入参数

            [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];

        }


 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值