Add object to NSMutableArray from other class

自作多情 提交于 2019-12-02 05:10:55

问题


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 @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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!