有多种方法可以实现这一点,但其中两种最简洁的方法是将参数传递给下一个视图控制器的自定义 init 方法,或者将参数设置为下一个视图的属性控制器。
请注意,这不仅限于在视图控制器之间传递数据 - 视图控制器是对象,任何对象都可以使用以下原则,我只是在以下示例中使用视图控制器。
这两个示例都使用简单的 UITableViewController 作为初始视图控制器,并且下一个视图控制器将传递给用户从单元格列表中选择的内容,用户可以在其中选择自己喜欢的颜色,以及他们的当前日期选择。这可以通过FROM任何类型的视图控制器TO任何类型的视图控制器进行一些小的修改,并且在合理的范围内,信息的类型/数量确实没有限制可以这样传递。
请记住,您可能不想要具有 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 - 创建/设置属性
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];
}
在这两种情况下,您现在都可以快速轻松地将多条信息从一个视图控制器传递到另一个视图控制器。