iOS - 属性设置器可以工作,但getter不在UICollectionViewController中

时间:2015-08-02 20:58:05

标签: ios objective-c iphone

我在UICollectionViewController中创建了一个属性placeId。在为其分配值后,我无法在当前viewController中获取该值。我无法找到原因。感谢您的帮助。

@property (strong, nonatomic) NSString *placeId;

-(void)setPlaceId:(NSString *)placeId{
   _placeId = placeId;
   [self.collectionView reloadData];
}

nslog的输出

2015-08-02 14:17:32.084 beautifulCity[1483:102947] placeid for viewdidload in collectionview = (null)
2015-08-02 14:17:33.439 beautifulCity[1483:102947] placeid for setter in collectionview= 2eIY2QFTVr_DwWZNLg
2015-08-02 14:17:33.439 beautifulCity[1483:102947] placeid for segue in tableviewcontroller = 2eIY2QFTVr_DwWZNLg

我通过segue为之前的uitableviewcontroller中的placeId属性赋值,但是我无法在UICollectionViewController的viewDidLoad中获取_placeId

- (void)viewDidLoad {
   [super viewDidLoad];
   self.collectionView.backgroundColor = [UIColor whiteColor];
   NSLog(@"placeid for new%@",_placeId);
   [self loadData];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
   if ([sender isKindOfClass:[UITableViewCell class]]) {
       NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
       if (indexPath) {
           if ([segue.identifier isEqualToString:@"showPhotos"]) {
             if ([segue.destinationViewController isKindOfClass:[UICollectionViewController class]]) {
                ViewController * destViewController = segue.destinationViewController;
                NSString *city = list[indexPath.row];                    
                NSURL *url = [FlickrFetcher getPlaceId:city];

                dispatch_queue_t fetchQ = dispatch_queue_create("flickr places fetcher", NULL);
                dispatch_async(fetchQ, ^{
                    NSData* JSONResults = [NSData dataWithContentsOfURL:url];
                    //NSLog(@"JSONResults%@",JSONResults);
                    NSDictionary* propertyListResults = [NSJSONSerialization JSONObjectWithData:JSONResults
                                                                                        options:0
                                                                                          error:NULL];

                    NSArray* places = [propertyListResults valueForKeyPath:FLICKR_RESULTS_PLACES];

                    NSDictionary *dict = places[0];
                    NSString *placeId = [dict objectForKey:FLICKR_PLACE_ID];

                    dispatch_async(dispatch_get_main_queue(), ^{                            
                        destViewController.placeId = placeId;

                    });
                });

            }
        }
    }
}

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试通常

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // DATA PROCESSING 1 
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI UPDATION 1
        });
});