我怎样才能从一个视图控制器传递信息到另一个?

这是一个Q&A风格的post

如果我想从一个视图控制器传递参数,如NSStringNSArrayNSDictionary ,那么最简单的方法是什么?

有多种方法可以实现这一点,但是其中两个最简洁的方法需要将parameter passing给下一个视图控制器的自定义init方法,或将参数设置为下一个视图控制器的属性。

请注意,这不限于在视图控制器之间传递数据 – 视图控制器是对象,任何对象都可以使用以下原则,下面的示例只是简单地使用视图控制器。

这两个例子都是使用一个简单的UITableViewController作为初始视图控制器,并且下一个视图控制器将被传递给用户从单元格列表中select他们喜欢的颜色以及当前select的date。 这可以任何types的视图控制器任何types的视图控制器进行一些小的修改,并且在合理的范围内对于可以以这种方式传递的信息的types/数量是没有限制的。

请记住,你可能不需要一个具有10个参数名称的大规模初始化方法,所以在这种情况下,你可能想拥有单独的属性并相应地设置每个属性。 如果只有一些参数,你也可能希望保持初始化/设置代码到一行,所以在这种情况下使用自定义的初始化方法可能适合你。

演示表视图设置

 #import "TestTableViewController.h" #import "NextViewController.h" @interface TestTableViewController () @end @implementation TestTableViewController - (void)viewDidLoad { [super viewDidLoad]; self.colors = @[@"Red", @"Orange", @"Yellow", @"Green"]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.colors.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ColorCell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ColorCell"]; } cell.textLabel.text = self.colors[indexPath.row]; return cell; } 

方法#1 – 自定义初始化程序

NextViewController.h

 #import <UIKit/UIKit.h> @interface NextViewController : UIViewController // expose the custom initializer so other view controller's can pass in the parameters we want to pass - (instancetype)initWithFavoriteColor:(NSString *)favoriteColor currentDate:(NSDate *)date; @end 

NextViewController.m

 #import "NextViewController.h" @implementation NextViewController // implement the custom initializer method - (instancetype)initWithFavoriteColor:(NSString *)favoriteColor currentDate:(NSDate *)date { self = [super init]; // do whatever you want here with the favorite color string and current date that // was passed in, such as save them to instance variables... return self; } @end 

在演示表视图中实现方法#1:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // get the color they selected for this row from our data source array NSString *selectedColor = self.colors[indexPath.row]; // initialize the next view controller using the handy init method we created NextViewController *nextVC = [[NextViewController alloc] initWithFavoriteColor:selectedColor currentDate:[NSDate date]]; [self.navigationController pushViewController:nextVC animated:YES]; } 

方法#2 – 创build/设置属性

NextViewController.h

 #import <UIKit/UIKit.h> @interface NextViewController : UIViewController @property (nonatomic, retain) NSString *favoriteColor; @property (nonatomic, retain) NSDate *currentDate; @end 

NextViewController.m

 #import "NextViewController.h" @implementation NextViewController - (void)viewDidLoad { [super viewDidLoad]; // do something here with your properties - by the time the view has loaded // they will have been set/passed from the original view controller self.favoriteColor... self.currentDate... } @end 

在演示表视图中实现方法#2:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // get the color they selected for this row from our data source array NSString *selectedColor = self.colors[indexPath.row]; // initialize the next view controller normally, and set its favorite color property // to be what the user selected NextViewController *nextVC = [NextViewController new]; nextVC.favoriteColor = selectedColor; nextVC.currentDate = [NSDate date]; [self.navigationController pushViewController:nextVC animated:YES]; } 

在这两种情况下,您现在都可以快速轻松地将多个信息从一个视图控制器传递到另一个视图控制器。