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

来源:互联网 发布:企业查询软件下载 编辑:程序博客网 时间:2024/06/05 06:05

接触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
原创粉丝点击