Search code examples
objective-cxcodepropertiesmethodscontrollers

Calling instance Method from another controller objective C


I have two files and I am trying to pass some information between them. Basically I have a Boolean Property that I use to decide what part of a method I should use. I created an instance method that returns this boolean property for viewing. In my first controller I create the property and set it and make the method and in my second controller I create an instance of the class and call the method to see what value the BOOL is. The problem is that I do not get the right answer when I call it, I always get 0. Can someone explain why? Thanks. In the Second File I have a mapView using annotations so the title property gets called on a click of the pin on the map

First File: TableViewController

#import "MyTableViewController.h"
#import "FlickrFetcher.h"
#import "myTableViewControllerPhotoLocation.h"
#import "FlickrImageViewController.h"
#import "FlickrPhotoSort.h"
#import "MapViewController.h"
#import "FlickrPhotoAnnotation.h"

@interface MyTableViewController () <MapViewControllerDelegate>
//This is declared in header
@property bool photoStage;
@end

@implementation MyTableViewController
@synthesize photoStage= _photoStage;

-(BOOL) currentPhotoStage{
NSLog(@"PhotoStage = %@", self.photoStage);
return self.photoStage;

}

-(void) setphotoStage: (bool) photoStage{
if(!_photoStage) _photoStage = NO;
else {
    _photoStage = photoStage;
}

 }

-(NSArray*) mapAnnotations{

NSMutableArray *annotations= [NSMutableArray arrayWithCapacity:[self.photoArray count]];
for(NSDictionary *photo in self.photoArray)
{
[annotations addObject:[FlickrPhotoAnnotation annotationForPhoto:photo]];
}
//I set the value of self.photoStage here
self.photoStage = YES;
//Prints out 1
NSLog(@"%d", self.photoStage);
return annotations;

 }

Second File called PhotoAnnotation

#import "FlickrPhotoAnnotation.h"
#import "FlickrFetcher.h"
#import "MapKit/Mapkit.h"
#import "MapViewController.h"
#import "MyTableViewController.h"

@interface FlickrPhotoAnnotation ()
@property (nonatomic,strong) MyTableViewController* mapCheck;
@end

@implementation FlickrPhotoAnnotation
@synthesize photo=_photo; //This is a dictionary
@synthesize mapCheck = _mapCheck;

Problem occurs here

-(NSString*) title{ //gets called on click of annotation in map
// ALWAYS RETURNS 0 hence if statement fails
NSLog(@"photoStage = %d", [self.mapCheck currentPhotoStage]);
if([self.mapCheck currentPhotoStage])
{
    NSLog(@"title = %@", [self.photo objectForKey:FLICKR_PHOTO_TITLE]);
    NSString *title = [self.photo objectForKey:FLICKR_PHOTO_TITLE];
    if([title isEqualToString:@""]) title = @"No Title";
    NSLog(@"title = %@", title);
    return title;
}else {

NSString * cellTitle = [self.photo objectForKey:@"_content"]; 

NSRange cellRange = [cellTitle rangeOfString:@","];

NSString * cellMainTitle = [cellTitle substringToIndex:cellRange.location];


return cellMainTitle;
//[self.photo objectForKey:FLICKR_PHOTO_TITLE];
}
}

Solution

  • The reason you always get 0 is because you've created a new instance of MyTableViewController -- it's not the same instance where you set the value (since properties are set to 0 or nil until you change them, that's why you get 0). You need to get a reference to your first view controller -- I'm not sure how, it depends on the overall structure of your app. Another approach would be to use notifications.