我又回来了,这将是我今天的第二个问题。
无论如何,我正在使用NSJSONSerialization
从我的网站解析数据。数据是数组格式,所以我使用NSMutableArray
. 问题是,我无法访问存储在NSMutableArray
不同视图控制器中的数据。
第一视图.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *firstViewControllerArray;
@end
第一视图.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];
}
第二个.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *secondViewControllerArray;
@end
秒.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Second: %@", [self secondViewControllerArray]);
}
输出
将NSLog(@"First: %@",array);
输出数组,因此它不会向 SecondViewController 传递(null)
数组的值。
但是,NSLog(@"Second: %@", [self secondViewControllerArray]);
将输出(null)
. 我错过了什么吗?