【发布时间】:2011-06-01 18:09:59
【问题描述】:
嗨 我有一个带有两个实例变量、NSString 和 NSDate 的对象(objectA)。 我还有另外两个对象 一个带有 tableView 和添加按钮 (objectB) 当您按下添加按钮 (objectC) 并在此对象视图中输入名称和日期时,会以模态方式呈现,当此对象 (objectC) 关闭时,我会创建带有名称和日期的新对象 (objectA)。
objectB 有 NSMutableArray,我想将 objectA 添加到这个数组中,以便它可以出现在 tableView 中,我在 objectC.m 中这样做
- (IBAction)saveButtonPressed {
objectA *a = [[objectA alloc] init];
[a setName:[myUITextField text]];
[a setDate:[myDatePicker date]];
objectB *b = [[objectB alloc] init];
[[b myMutableArray] addObject:a]];
[[b myMutableArray] count]; // count == 1 here but when i go back to objectB implementation it will be 0
}
应用程序在此处崩溃
有什么想法吗?
谢谢,
编辑: 崩溃消失了,我只是编辑了 objectA 的 init 方法 但 myMutableArray 仍为 0
在objectB.m中
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addBarButton:)];
[navItem setRightBarButtonItem:bbi];
[bbi release];
myMutableArray = [[NSMutableArray alloc] init];
}
return self;
}
- (void)addBarButton:(id)sender {
myObjectC = [[objectC alloc] init];
[self presentModalViewController:myObjectC animated:YES];
}
我在 objectB.h 中也有
@property (nonatomic, retain) NSMutableArray *myMutableArray;
saveButtonPressed 方法中 objectC 中 myMutableArray 的计数现在为 1 但是当我回到我想显示 myMutableArray 的 objectB tableView 仍然是 0
编辑2: 在我放了很多 NSLog 之后 我发现当我在 saveButtonPressed: 方法中创建新 objectB 时,myMutableArray 的计数将为 1 但是当我回到我的 tableView (objectB) myMutableArray 回到 0 可能是因为我在 objectC 中创建了 objectB 的新的和单独的对象(saveButtonPressed :) 还 如果我不在 saveButtonPressed: 方法中分配和初始化 objectB objectB 将为零,我不能将 objectA 放入 myMutableArray
所以我想我必须获得指向原始 objectB 的指针,但如何?
【问题讨论】:
-
这一行:[[b myMutableArray] addObject:a]];有太多的方括号:) - 我看不到关于这段代码的任何其他明显的东西,看到你的实例变量会非常有用 - 特别是 NSMutableArray 被定义和实例化。
-
请发布触发您问题的实际代码,而不是乱七八糟的
myObjectXYZABC123混淆。如果您无法使用实际代码,请以您可以与我们分享的形式重现崩溃。 -
抱歉混淆了,但这正是我在 xcode 中命名它的方式,除了 myMutableArray 是我代码中的任务
标签: iphone objective-c cocoa-touch ios4 nsmutablearray