从其他类添加对象到NSMutableArray [英] Add object to NSMutableArray from other class
问题描述
我有一个名为 PresidentsViewController
的视图控制器类,用于在 UITableView
中设置数据。此数据的格式为 NSMutableArray
,名为 list
。我有另一个类 PresidentAddController
,它应该处理根据用户添加类型为 President
的对象到此列表输入有关总统的数据。但是,我无法将对象添加到列表中。我已经确认正在正确收集用户为新总统输入的数据,因此它正在添加到导致问题的另一个类的列表中。我相信将对象添加到列表中的正确代码是:
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];
但是,我不知道如何正确创建引用/实例/? (这是pvc将是什么)到PresAdadController内部的PresidentsViewController,以便我可以正确地将新的总统添加到列表中。我没有使用Interface Builder,因为它只是一个UITableView。
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?
编辑:以下是数组的初始化方式:
Here is how the array is being initialized:
@property (nonatomic, retain) NSMutableArray *list;
以下是在PresidentsViewController中设置PresidentAddController的方法:
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];
推荐答案
的指针添加到 PresidentAddController
这样:
// in @interface
PresidentsViewController *listController;
@property (nonatomic, assign) PresidentsViewController *listController;
// in @implementation
@synthesize listController;
然后当您实例化 PresidentAddController
时,设置指针:
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];
那么你可以去 [listController.list addObject:newPresident];
in PresidentAddController
。
So then you can go [listController.list addObject:newPresident];
in PresidentAddController
.
编辑: childController.listController =自
调用 [childController setListController:自]
,这反过来读取 @synthesize 你的实现中的code> d方法并设置指针
* listController
指向当前类(如果你在中编写代码PresidentsViewController
类,然后 self
将成为 PresidentsViewController
的当前实例。
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
).
我使用 assign
的原因是因为如果你要使用 retain
,然后当你将 listController
设置为 self
时,它实际上会保留对该对象的拥有引用。这可能会导致各种各样的问题,如果你曾经尝试取消分配 PresidentsViewController
,因为如果你有在 PresidentAddController
一个拥有引用然后它不会解除分配,直到该引用也被释放。使用 assign
确保在 PresidentAddController
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.
这篇关于从其他类添加对象到NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!