【问题标题】:How can I pass information from one view controller to another? [duplicate]如何将信息从一个视图控制器传递到另一个? [复制]
【发布时间】:2014-08-28 03:14:14
【问题描述】:

这是一个问答风格的帖子

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

【问题讨论】:

  • 是的,这个问题以前曾被问过,但也经常被问到,并且根据 Stack Exchange 博客“不仅可以提出和回答您自己的问题,而且明确鼓励这样做”,其中是我在这里所做的。这是有目的地完成的,“问答风格”,以便为一个非常常见的问题提供我认为非常清晰和简洁的答案,这个问题确实已经有了很好的答案,但我认为我的格式是一种新的有用的格式。跨度>
  • 最简单的方法就是简单地通过它。 VC A 在初始化时将参数传递给 VC B,或者将参数分配给 VC B 的属性,或者调用 VC B 的方法,传递参数。或者 VC B 可以作为委托获得指向 A 的指针,并且可以回调 A 以获取参数。 (我还不明白为什么这被认为是一个如此复杂的问题。)
  • @HotLicks 因此我在下面的回答 ;) 我仍然经常看到这个问题,所以我想尝试给出比以前更详细的答案。
  • 但是(不言而喻的)问题不是更常见“如果我的应用程序设计不佳或者我使用某种框架掩盖了信息?”
  • 嗯,作为一个离“菜鸟”不远的人,我一般倾向于不同意,但我相信在很多情况下都是如此。

标签: ios objective-c cocoa-touch uiviewcontroller


【解决方案1】:

有多种方法可以实现这一点,但其中两种最简洁的方法是将参数传递给下一个视图控制器的自定义 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];
}

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

【讨论】:

  • 你真的不应该说“可以从任何类型的 iOS 对象 到任何类型的 iOS 对象 完成吗?绝对没有什么特别之处在这方面查看控制器。
  • 当然,但在我看来,大多数 新程序员尤其会遇到这个特定问题。我会添加注释。
  • 一个大问题是,刚接触 Objective-C 的程序员似乎在某种程度上认为视图控制器与其他对象不同。
猜你喜欢
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多