ios - Objective C - Passing data between two UITableViewControllers -
this question has answer here:
- passing data between view controllers 35 answers
display details in second uitableviewcontroller
my application simple recipebook understand uitableviewcontrollers. application contains 2 uitableviewcontrollers. first uitableviewcontroller contains uitableview list of recipe names. if select cell segue second uitableviewcontroller. second uitableviewcontroller contains uitableview list of ingredients.
the application contains following classes:
- recipetableviewcontroller (first)
- ingredienttableviewcontroller (second)
- recipeobject
- recipedata
the recipeobject class contains 2 properties. 1 property of type nsstring recipe name. other property of type nsarray ingredients. recipeobject objects in 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];
the recipedata called in recipetableviewcontroller display recipe names in tableview.
message recipedata class recipetableviewcontroller:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. self.recipes = [recipedata allrecipes];}
display names in 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; }
how add recipe1.ingredients array ingredientstableviewcontroller?
any highly appreciated!
perform segue when recipe selected. in prepare segue-method take selected recipe , pass ingredients ingredientstableviewcontroller. quite similar this:
-(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{ [self performseguewithidentifier:@"to_ingredients" sender:self]; } // in storyboard-based application, want 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; } }
if don't use storyboard set segue need first method should looks this:
-(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] }
Comments
Post a Comment