2014-05-23 2 views
-1

NSMutableArray 다른 클래스에서 액세스하려고하는데 다음 코드를 사용할 때 코드를 호출 할 때 값이 없습니다.다른 클래스의 NSMutableArray에 액세스

TableViewController.h

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController 

@property (nonatomic, strong) NSMutableArray *imageURLs; 

@end 

TableViewController.m

#import "TableViewController.h" 
#import "ImageCell.h" 
#import "ParseJSON.h" 
#import "AsyncImageView.h" 

@implementation TableViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ParseJSON *json = [[ParseJSON alloc] init]; 
    [json Parse]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    ImageCell *cell = [[ImageCell alloc] init]; 

    cell.firstImage.imageURL = self.imageURLs[0]; 
    cell.secondImage.image = self.imageURLs[1]; 
    cell.thirdImage.image = self.imageURLs[2]; 

    return cell; 
} 
@end 

ParseJSON.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "TableViewController.h" 

@interface ParseJSON : NSObject 

- (void)Parse; 

@end 

ParseJSON.m

#import "ParseJSON.h" 

@implementation ParseJSON 

- (void)Parse 
{ 
    NSData *imageData = [[NSData alloc] initWithContentsOfURL: 
           [NSURL URLWithString:@"http://www.delta.rhystowey.com/json/main.json"]]; 
    NSError *error; 
    NSDictionary *imageDictionary = [NSJSONSerialization 
           JSONObjectWithData:imageData 
           options:kNilOptions 
           error:&error]; 
    NSMutableArray *URLs = [NSMutableArray array]; 

    if(error) 
    { 
     NSLog(@"%@", [error localizedDescription]); 
    } 
    else { 
     NSArray *images = imageDictionary[@"Images"]; 
     for (NSDictionary *image in images) 
     { 
      [URLs addObject:image[@"url"]]; 
     } 
    } 
    TableViewController *tvc = [[TableViewController alloc] init]; 
    tvc.imageURLs = URLs;  
} 
@end 
+0

귀하의 질문에 정확히 무엇입니까 viewDidLoad에서 다음

- (NSMutableArray *)parse { NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.delta.rhystowey.com/json/main.json"]]; NSError *error; NSDictionary *imageDictionary = [NSJSONSerialization JSONObjectWithData:imageData options:kNilOptions error:&error]; NSMutableArray *URLs = [NSMutableArray array]; if(error) { NSLog(@"%@", [error localizedDescription]); } else { NSArray *images = imageDictionary[@"Images"]; for (NSDictionary *image in images) { [URLs addObject:image[@"url"]]; } } return URLs; } @end 

? '? '로 끝나는 질문에 유의하십시오. – Popeye

+0

완전히 새로운'tvc' 객체를 만든 것 같습니다. 그것은 당신이 생각하는 것이 아닙니다. 포인터인지 확인하십시오. 포인터가 다르다는 것을 알 수 있어야합니다. – Larme

답변

2

하면 도면 컨트롤러 만들기 및 배열을 설정한다. 그 후, 그것은 공개된다. 기본적으로이 두 줄은 잘못되었습니다.

TableViewController *tvc = [[TableViewController alloc] init]; 
tvc.imageURLs = URLs; 

배열을 반환하도록 구문 분석 방법을 변경하십시오.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ParseJSON *json = [[ParseJSON alloc] init]; 
    _imageURLs = [json Parse]; 
} 
관련 문제