我有兩個文件,我試圖在它們之間傳遞一些信息。基本上我有一個布爾屬性,我用它來決定我應該使用什麼方法的一部分。我創建了一個實例方法,該方法返回此布爾值屬性以供查看。在我的第一個控制器中,我創建了屬性並設置了它,並創建了方法,並在第二個控制器中創建了該類的一個實例,並調用該方法來查看BOOL的值。問題是我打電話時沒有得到正確的答案,我總是得到0.有人可以解釋爲什麼嗎?謝謝。在第二個文件我有使用說明一個MapView等多項稱號屬性,都會叫上在銷的地圖上點擊從另一個控制器目標調用實例方法C
第一個文件: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;
}
稱爲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;
第二個文件
這裏發生的問題
-(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];
}
}
0是bool數據類型的默認值。所以,如果它沒有設置,那麼它總是會出現0. – Dustin 2012-08-13 02:48:31