I have a view controller class called PresidentsViewController that sets up data in a UITableView. This data is in the form of NSMutableArray called list. I have another class, PresidentAddController, that is supposed to handle adding an object of type President to this list based on user inputted data about a president. However, I can’t get the object to add to the list. I have confirmed that the user inputted data for a new president is being collected correctly, so it is adding to the list that’s in another class that’s causing problems. I believe the correct code to add an object to the list is:
[pvc.list addObject:newPresident];
However, I don’t know how to properly create the reference/instance/? (which is what pvc would be) to PresidentsViewController inside of PresidentAddController so that I can properly add a new president to the list. I am not using Interface Builder for this because it is just a UITableView.
How do I add a president to the list in this situation?
Edit: Here is how the array is being initialized:
@property (nonatomic, retain) NSMutableArray *list;
And here is how PresidentAddController is being set up in PresidentsViewController:
PresidentAddController *childController = [[PresidentAddController alloc] initWithStyle:UITableViewStyleGrouped];
childController.title = @"Add President";
[self.navigationController pushViewController:childController animated:YES];
[childController release];
Add a pointer to
PresidentAddControllerthis way:Then when you instantiate your
PresidentAddController, set the pointer:So then you can go
[listController.list addObject:newPresident];inPresidentAddController.EDIT:
childController.listController = selfcalls[childController setListController:self], which in turn reads the@synthesized method in your implementation and sets the pointer*listControllerto point to the current class (if you’re writing code in thePresidentsViewControllerclass, thenselfis going to be the current instance ofPresidentsViewController).The reason why I use
assignis because if you were to useretain, then when you setlistControllertoselfit will actually keep an owning reference to the object. This can cause all sorts of problems if you ever try to deallocatePresidentsViewController, because if you have an owning reference inPresidentAddControllerthen it will not deallocate until that reference is also released. Usingassignensures that if you ever release thePresidentsViewControllerbeforePresidentAddControllerdisappears, it will be properly deallocated. Of course, maybe you want to keep it around in that situation, in which case usingretainhere is also fine.