0

我在将信息从一个视图控制器输入到另一个视图控制器时遇到问题。此函数位于第二个视图控制器中,并在第一个控制器中被多次调用,因为它位于 for 循环中,并且我想将多个对象添加到数组 transfer_array 中。对象是 Poi 类型,由 imageLocationX、imageLocationY 和名称组成。我有 BOOL 的原因是我尝试只初始化数组一次。第一次调用函数时将 Poi 对象添加到数组中,再次调用函数后数组为空。

- (id)display:(double)imageXX andY:(double)imageYY withName:(NSString *)namee ifDone:(BOOL *)donee{
    NSLog(@"````````````````````````````````````````````````````````");

    NSLog(donee ? @"Yes" : @"No");
    if(donee == NO){
        transfer_array = [[NSMutableArray alloc] init];
    }

    NSLog(@"imageX: %f",imageXX);
    NSLog(@"imageY: %f", imageYY);
    NSLog(@"name: %@", namee);

    labelPoi = [[Poi alloc] init];
    labelPoi.imageLocationX = imageXX;
    labelPoi.imageLocationY = imageYY;
    labelPoi.name = namee;
    [transfer_array addObject:labelPoi];


    NSLog(@"label.x: %f should be: %f", labelPoi.imageLocationX, imageXX);
    NSLog(@"label.y: %f should be: %f", labelPoi.imageLocationY, imageYY);
    NSLog(@"label.name: %@ should be: %@",labelPoi.name,namee);
    NSLog(@"transssssfer: %lu", (unsigned long)transfer_array.count);

    return self;
}

这是第一次调用该函数时的日志:

````````````````````````````````````````````````````````
2013-07-29 13:25:52.502 App[20856:11303] No
2013-07-29 13:25:52.502 App[20856:11303] imageX: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] imageY: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] name: Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] label.x: 979.008057 should be: 979.008057
2013-07-29 13:25:52.503 App[20856:11303] label.y: 115.728180 should be: 115.728180
2013-07-29 13:25:52.503 App[20856:11303] label.name: Urgent Care should be:  Urgent Care
2013-07-29 13:25:52.503 App[20856:11303] transfer_array.count: 1

第二次:

2013-07-29 13:25:52.506 App[20856:11303] ````````````````````````````````````````````````````````
2013-07-29 13:25:52.506 App[20856:11303] Yes
2013-07-29 13:25:52.506 App[20856:11303] imageX: 224.485718
2013-07-29 13:25:52.506 App[20856:11303] imageY: 116.353401
2013-07-29 13:25:52.506 App[20856:11303] name: Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] label.x: 224.485718 should be: 224.485718
2013-07-29 13:25:52.507 App[20856:11303] label.y: 116.353401 should be: 116.353401
2013-07-29 13:25:52.507 App[20856:11303] label.name: Student Health Center should be:  Student Health Center
2013-07-29 13:25:52.507 App[20856:11303] transfer_array.count: 0

我无法访问数组中的任何信息,因为它是空的。有谁知道我如何修改它,以便该函数不断添加我想要添加的对象而不是保持为空?

编辑

这就是在第一个视图控制器中调用该函数的方式,这个方法是一个朋友向我推荐的

PictureViewController *newlabel = [[PictureViewController alloc] display:PointOfInterest.imageLocationX andY:PointOfInterest.imageLocationY withName:PointOfInterest.name ifDone:done];
            if(done == NO){
                done = YES;
            }
4

2 回答 2

1

叹!!完全删除

NSLog(donee ? @"Yes" : @"No");
if(donee == NO){
    transfer_array = [[NSMutableArray alloc] init];
}

如果您添加了它,请完全删除它:

// property getter for transfer_array: lazily load the object
- (NSArray *)transfer_array
{
    return (transfer_array ?: transfer_array = [[NSMutableArray alloc] init];
}

添加这个:

-(id)init {
   self = [super init];
   if (self) {
       self.transfer_array = [NSMutableArray array];
   }
}

-(void)dealloc {
   self.transfer_array = nil;
   // If you are not using ARC include this line
   [super dealloc];
   // (If you're not sure, add it and then remove it if the compiler complains about it.)
}

将所有剩余的“transfer_array”更改为“self.transfer_array”。

于 2013-07-29T20:16:56.777 回答
1

尽管 esker 的回答确实消除了完成的必要性,因此更可取,但这不是您的错误所在。您遇到的问题是,每次您通过循环时,您都在初始化一个新的 PictureViewController 实例。在这个新对象中,尚未创建数组,因此它为 (null),返回计数为 0。

您需要做的是创建 viewController 的一个实例,然后将所有对象添加到其中。此外,在 init 方法中,您想要调用 [super init] 或超级初始化程序的另一个版本,或者您拥有的另一个初始化程序调用超级初始化程序。

于 2013-07-29T19:16:12.403 回答