Reputation: 325
I am having an issue with inputting information from one view controller into another. This function is in the second viewcontroller and is called multiple times in the first controller as it is in a for loop and there are multiple objects I want added to the array transfer_array. The objects are of type Poi and consist of a imageLocationX, imageLocationY, and name. The reason I have the BOOL was my attempt at only initializing the array once. The Poi object is added to the array the first time the function is called and the array is empty after the function is called again.
- (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;
}
Here is the log from the first time the function is called:
````````````````````````````````````````````````````````
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
And the second time:
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
I cannot access any of the information in the array because it is empty. Does anyone know how I can modify this so that the function will continually add the objects I want to add instead of remaining empty?
EDIT
This was how the function was called in the first view controller and this method was suggested to me by a friend
PictureViewController *newlabel = [[PictureViewController alloc] display:PointOfInterest.imageLocationX andY:PointOfInterest.imageLocationY withName:PointOfInterest.name ifDone:done];
if(done == NO){
done = YES;
}
Upvotes: 0
Views: 304
Reputation: 47729
Sigh!! Completely remove
NSLog(donee ? @"Yes" : @"No");
if(donee == NO){
transfer_array = [[NSMutableArray alloc] init];
}
Completely remove this, if you've added it:
// property getter for transfer_array: lazily load the object
- (NSArray *)transfer_array
{
return (transfer_array ?: transfer_array = [[NSMutableArray alloc] init];
}
Add this:
-(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.)
}
Change all remaining occurrences of "transfer_array" to "self.transfer_array".
Upvotes: 1
Reputation: 5312
Although esker's answer does remove the neccessity of the done and is thus preferable, that is not where your error was located. The problem that you were having was that each time you are going through your loop, you are initializing a new instance of PictureViewController. In this new object, the array has not yet been created and thus it is (null), returning a count of 0.
What you need to do is create one instance of the viewController and then add all of the objects to it. Also, in an init method, you want to call [super init] or another version of the super's initializer or another initializer you have that calls a super initializer.
Upvotes: 1