Objective C - 在两个 UITableViewController 之间传递数据
Objective C - Passing data between two UITableViewControllers
在第二个 UITableViewController 中显示详细信息
我的应用程序是一个简单的 Recipebook 来理解 UITableViewControllers。该应用程序包含两个 UITableViewController。第一个 UITableViewController 包含一个带有菜谱名称列表的 UITableView。如果您 select 一个单元格,您将转到第二个 UITableViewController。第二个 UITableViewController 包含一个带有成分列表的 UITableView。
该应用程序包含以下 classes:
- RecipeTableViewController(第一个)
- IngredientTableViewController(第二个)
- 食谱对象
- 配方数据
RecipeObject Class 包含两个属性。一个 属性 类型的 NSString 和配方名称。另一个 属性 是带有成分的 NSArray 类型。 RecipeObject 对象位于 RecipeData class 中。
RecipeObject *recipe1 = [[RecipeObject alloc]init];
recipe1.name = @"Fresh Coconut Cake";
recipe1.ingredients = [NSArray arrayWithObjects:@"Coconut cups", @"Milk", @"Baking powder", @"Butter", @"Sugar", @"Eggs", nil];
在 RecipeTableViewController 中调用 RecipeData 以在 tableView 中显示食谱名称。
从 RecipeData class 到 RecipeTableViewController 的消息:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.recipes = [RecipeData allRecipes];}
在 tableView 中显示姓名:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"recipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
RecipeObject *recipes = [self.recipes objectAtIndex:indexPath.row];
cell.textLabel.text = recipes.name;
return cell;
}
如何将 recipe1.ingredients 数组添加到 IngredientsTableViewController?
非常感谢任何帮助!
选择菜谱后执行 segue。在为 segue 方法做准备时,获取选定的食谱并将配料传递给 ingredientsTableViewController。
与此非常相似的内容:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"to_Ingredients" sender:self];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"to_Ingredients"]) {
IngredientTableViewController *ingredientsTableViewController = (IngredientTableViewController *)[segue destinationController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
ingredientsTableViewController.ingredients = recipe.ingredients;
}
}
如果您不使用故事板来设置此 segue,您只需要第一个方法,它应该如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
IngredientTableViewController *ingredientsTableViewController = [[IngredientTableViewController alloc]init];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
ingredientsTableViewController.ingredients = recipe.ingredients;
[self showDetailViewController:ingredientsTableViewController sender:self]
}
在第二个 UITableViewController 中显示详细信息
我的应用程序是一个简单的 Recipebook 来理解 UITableViewControllers。该应用程序包含两个 UITableViewController。第一个 UITableViewController 包含一个带有菜谱名称列表的 UITableView。如果您 select 一个单元格,您将转到第二个 UITableViewController。第二个 UITableViewController 包含一个带有成分列表的 UITableView。
该应用程序包含以下 classes:
- RecipeTableViewController(第一个)
- IngredientTableViewController(第二个)
- 食谱对象
- 配方数据
RecipeObject Class 包含两个属性。一个 属性 类型的 NSString 和配方名称。另一个 属性 是带有成分的 NSArray 类型。 RecipeObject 对象位于 RecipeData class 中。
RecipeObject *recipe1 = [[RecipeObject alloc]init];
recipe1.name = @"Fresh Coconut Cake";
recipe1.ingredients = [NSArray arrayWithObjects:@"Coconut cups", @"Milk", @"Baking powder", @"Butter", @"Sugar", @"Eggs", nil];
在 RecipeTableViewController 中调用 RecipeData 以在 tableView 中显示食谱名称。
从 RecipeData class 到 RecipeTableViewController 的消息:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.recipes = [RecipeData allRecipes];}
在 tableView 中显示姓名:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"recipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
RecipeObject *recipes = [self.recipes objectAtIndex:indexPath.row];
cell.textLabel.text = recipes.name;
return cell;
}
如何将 recipe1.ingredients 数组添加到 IngredientsTableViewController?
非常感谢任何帮助!
选择菜谱后执行 segue。在为 segue 方法做准备时,获取选定的食谱并将配料传递给 ingredientsTableViewController。 与此非常相似的内容:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"to_Ingredients" sender:self];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"to_Ingredients"]) {
IngredientTableViewController *ingredientsTableViewController = (IngredientTableViewController *)[segue destinationController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
ingredientsTableViewController.ingredients = recipe.ingredients;
}
}
如果您不使用故事板来设置此 segue,您只需要第一个方法,它应该如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
IngredientTableViewController *ingredientsTableViewController = [[IngredientTableViewController alloc]init];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
ingredientsTableViewController.ingredients = recipe.ingredients;
[self showDetailViewController:ingredientsTableViewController sender:self]
}