您的位置:首页 > 其它

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

2014-02-23 23:13 471 查看
接触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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: