一、总结几种关于ViewControllers之间传值的方式(从父视图向子视图传值)

接触ios时间比较短,遇到很多问题,ViewController之间的传值方式是其中一个比较疑惑的问题之一

经查阅apple developer library,总结出以下几种传值方式(若有错误或者不对之处,请不必留情直接指出):

以FirstViewController、SecondViewContrller两个视图举例:

一、将FirstViewController的值 传到 SecondViewController  (从父视图向子视图传值)

1、在secondViewController中创建@property

//SecondViewController.h  中创建@property
@property (nonatomic) NSString *data;

//在FirstViewController.m中导入
#import "SecondViewController.h"

//在需要创建SecondViewViewController视图并需传值时
SecondViewController *secondViewContrller = [[SecondViewController alloc]
 initWithNibName: @"SecondViewControlle" bundle:nil];
secondViewController.data = @"Passing Data Forward";
[self.view addSubview:secondViewController.view]

2、在secondViewController中创建变量和操作方法


//在SecondViewController.h中加入
@interface SecondViewController : UIViewController{
	NSString *data;
}
- (void)setData: (NSString *)set;

//在SecondViewController.m中加入
- (void)setData: (NSString *)set{
	data = set;
}

//在FirstViewController.m中
#import "SecondViewController.h"

//创建SecondViewController视图
SecondViewController *secondViewContrller = [[SecondViewController alloc]
 initWithNibName: @"SecondViewControlle" bundle:nil];
[secondViewController setData:@"Passing Value"];
[self.view addSubview:secondViewController.view];


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值