1

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];
2
  • Can you post your code where you initialize your mutable array and set it s properties? Commented May 18, 2011 at 4:37
  • I just edited the question to include that. Commented May 18, 2011 at 4:59

2 Answers 2

1

Add a pointer to PresidentAddController this way:

// in @interface
PresidentsViewController *listController;

@property (nonatomic, assign) PresidentsViewController *listController;

// in @implementation
@synthesize listController;

Then when you instantiate your PresidentAddController, set the pointer:

PresidentAddController *childController = 
  [[PresidentAddController alloc]
   initWithStyle:UITableViewStyleGrouped];
childController.title = @"Add President";
childController.listController = self;
[self.navigationController pushViewController:childController animated:YES];
[childController release];

So then you can go [listController.list addObject:newPresident]; in PresidentAddController.

EDIT: childController.listController = self calls [childController setListController:self], which in turn reads the @synthesized method in your implementation and sets the pointer *listController to point to the current class (if you're writing code in the PresidentsViewController class, then self is going to be the current instance of PresidentsViewController).

The reason why I use assign is because if you were to use retain, then when you set listController to self it will actually keep an owning reference to the object. This can cause all sorts of problems if you ever try to deallocate PresidentsViewController, because if you have an owning reference in PresidentAddController then it will not deallocate until that reference is also released. Using assign ensures that if you ever release the PresidentsViewController before PresidentAddController disappears, it will be properly deallocated. Of course, maybe you want to keep it around in that situation, in which case using retain here is also fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response -- see my edits for how PresidentAddController is being initialized. Yes to your first question.
Updated the answer following your clarifications :)
This works! Would you mind explaining exactly what childController.listController = self; does? That seems to be the line of code I needed. Also, for the @property declaration, was it necessary to make it "assign" because you were going to be setting it to "self"?
0

My suspicion here would be that you have a property defined as @property (nonatomic,copy) NSMutableArray *list; Are you getting and exception trying to add the object into the array? If so, it's likely because the copy modifier is returning an immutable copy of the array. Try to create a method that takes the object and adds it to the list without using self.list ... just [list addObject] and if that works then this is your problem.

1 Comment

No, I'm not getting an exception. I believe it's a problem with accessing the class PresidentsViewController in the first place, but I'm not sure. What would be the correct way of setting up the reference/instance without having to alloc/init PresidentsViewController again because that does not work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.