问题
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];
回答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 @synthesize
d 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.
回答2:
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.
来源:https://stackoverflow.com/questions/6039841/add-object-to-nsmutablearray-from-other-class