私はまた戻ってきました。これがその日の2番目の質問になります。
とにかく、私はNSJSONSerialization
自分のWebサイトからのデータを解析するために使用しています。データは配列形式なので、を使用してNSMutableArray
います。問題は、別のViewControllerから保存されているデータにアクセスできないことNSMutableArray
です。
FirstView.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *firstViewControllerArray;
@end
FirstView.m
- (void)ViewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://j4hm.t15.org/ios/jsonnews.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
self.firstViewControllerArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}];
[self loadArray];
}
- (void)loadArray
{
NSMutableArray *array = [NSMutableArray arrayWithArray:[self firstViewControllerArray]];
//I also tried codes below, but it still won't work.
//[[NSMutableArray alloc] initWithArray:[self firstViewControllerArray]];
//[NSMutableArray arrayWithArray:[self.firstViewControllerArray mutableCopy]];
NSLog(@"First: %@",array);
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[secondViewController setSecondViewControllerArray:array];
[[self navigationController] pushViewController:secondViewController animated:YES];
}
Second.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *secondViewControllerArray;
@end
Second.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Second: %@", [self secondViewControllerArray]);
}
出力
はNSLog(@"First: %@",array);
配列を出力するため、SecondViewController(null)
に配列の値を渡しません。
ただし、NSLog(@"Second: %@", [self secondViewControllerArray]);
を出力します(null)
。私は何かが足りないのですか?