如何将信息从一个视图控制器传递到另一个? [英] How can I pass information from one view controller to another?

查看:59
本文介绍了如何将信息从一个视图控制器传递到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想传递参数,例如 NSString NSArray NSDictionary ,从一个视图控制器到另一个视图控制器,最简单的方法是

If I want to pass parameters, such as an NSString, NSArray, or NSDictionary, from one view controller to another, what is the easiest way to do this?

推荐答案

有多种方法可以实现此目的,但是最干净的方法中有两个涉及传递参数( s)设置为下一个视图控制器的自定义init方法,或将参数设置为下一个视图控制器的属性。

There are multiple ways to achieve this, but two of the cleanest methods would involve passing the parameter(s) into a custom init method for the next view controller, or setting the parameter(s) as a property of the next view controller.

请注意,这不限于在视图控制器之间传递数据-视图控制器是对象,任何对象都可以使用以下原理,我只是在以下示例中使用视图控制器。

Note that this is not restricted to passing data between view controllers - view controllers are objects and any objects can use the following principles, I'm simply using view controllers for the following examples.

这两个示例都使用一个简单的UITableViewController作为初始视图控制器,下一个视图控制器将通过用户从单元格列表中的选择传递给用户他们可以选择自己喜欢的颜色以及选择的当前日期。可以通过 FROM 任何类型的视图控制器 TO 进行任何少量的修改即可完成,并且在一定程度上,信息的类型/数量实际上没有限制可以通过这种方式传递。

Both of these examples are using a simple UITableViewController as the initial view controller, and the next view controller will be passed the user's selection from a list of cells where they can choose their favorite color, as well as the current date of their selection. This can be done FROM any type of view controller TO any type of view controller with a few minor modifications, and within reason there's really no limit to the types/quantity of information that can be passed this way.

请记住,您可能不希望使用带有10个参数名称的大规模初始化方法,因此在这种情况下,您可能希望拥有各个属性,并相应地设置每个属性。如果只有几个参数,您可能还希望将初始化/设置代码保持在一行,因此在这种情况下,可以使用自定义初始化方法。

keep in mind that you likely don't want a massive initializer method with 10 parameter names, so in that case you might want to have individual properties and set each one accordingly. You also might want to keep the initialization/setup code to a single line if there's only a few parameters, so using a custom initializer method might be for you in that case.

#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



Method #1 - Custom Initializer

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



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演示表视图:



Implement method #1 in our demo table view:

- (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



Method #2 - Creating/Setting Properties

NextViewController.h

#import <UIKit/UIKit.h>

@interface NextViewController : UIViewController

@property (nonatomic, retain) NSString *favoriteColor;
@property (nonatomic, retain) NSDate *currentDate;

@end



NextViewController.m



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:



Implement method #2 in our demo table view:

- (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];
}

在这两种情况下,您现在都可以快速,轻松地传递来自一个视图控制器转到另一个。

In both instances, you have now quickly and easily passed multiple pieces of information from one view controller to another.

这篇关于如何将信息从一个视图控制器传递到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆