iphone - Add Objects From One View Controller To An Array On Another View Controller -


i have been trying figure out while , not coming solution. have view controller table , first cell of table allocated button called "add friends". when clicked, takes view controller list of contacts in table. when click on person, goes other view controller , adds selected person. have far.

contactsviewcontroller.m

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { firstviewcontroller *newvc = [self.storyboard instantiateviewcontrollerwithidentifier:@"newvcsegue"];  newvc.peoplearray = [[nsmutablearray alloc] init];  person *user = [contactslist objectatindex:indexpath.row]; nsarray *userkeys = [nsarray arraywithobjects:@"firstname", @"lastname", nil]; nsarray *userobjects = [nsarray arraywithobjects:user.firstname, user.lastname, nil]; nsdictionary *userdictionary = [nsdictionary dictionarywithobjects:userobjects forkeys:userkeys]; [newvc.peoplearray addobject:userdictionary];  [self.navigationcontroller pushviewcontroller:newvc animated:yes];  [tableview deselectrowatindexpath:indexpath animated:yes]; } 

firstviewcontroller.h

@property (strong, nonatomic) nsmutablearray *peoplearray; 

firstviewcontroller.m

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { //... if (indexpath.row == 0) {         contactname.text = @"add person";         imgview.image = [uiimage imagenamed:@"plus-icon.png"]; } else { nsstring *firstname = [[peoplearray objectatindex:(indexpath.row)-1] objectforkey:@"firstname"]; nsstring *lastname = [[peoplearray objectatindex:(indexpath.row)-1] objectforkey:@"lastname"]; contactname.text = [nsstring stringwithformat:@"%@ %@", firstname, lastname]; } return cell; } 

this lets me add 1 friend far , if decided add list, replaces first friend added.

what's happening every time select new contact, you're recreating array in first view controller, hence replacing things. ideally want try , avoid getting firstviewcontroller using storyboard well, it's pretty bad practice , may lead various problems later.

what i'd suggest in situation creating protocol (look @ delegate pattern). way, you'd have :

  • use taps "add contact"
  • contacts list appears, , firstviewcontroller set delegate
  • user taps contact add them
  • contactsviewcontroller informs delegate of user selected
  • firstviewcontroller adds user, , dismissed view controller

this approach you'd take, , it's pretty simple implement. start protocol

@protocol contactsdelegate -(void) contactsviewcontroller:(contactsviewcontroller *)vc didselectcontact:(person *)person; @end 

then, make firstviewcontroller implement protocol. this, in header file, in angle brackets after name (< >) add contactsdelegate

in implementation of firstviewcontroller, add new method of contacts delegate.

in contactsviewcontroller.h file, add

@property (nonatomic, assign) nsobject<contactsdelegate> *delegate; 

then when display contacts view controller, set delegate

uservc.delegate = self; [self presentmodalviewcontroller:uservc]; 

then, in user view controllers didselectrowatindexpath:, inform delegate you've selected person

[delegate contactsviewcontroller:self didselectcontact:[contactslist objectatindex:indexpath.row]]; 

and lastly, in firstviewcontroller, in delegate method added, need add user list, not re-create list

[peoplearray addobject:person]; 

and should you're after :)


Comments