如何从我的FirstViewController访问NSMutableArray

时间:2013-02-02 06:03:30

标签: iphone ios objective-c ipad uiviewcontroller

我能够解析JSON并将其存储在NSMutableArray中名为json的FirstViewController中。问题是,我不知道如何从我的视图控制器访问FirstViewController中的数组。

修改!!!

我将代码从AppDelegate移到了我的FirstViewController,因为这不是一个好习惯。

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
    NSMutableArray *jsonData;
}

@property (nonatomic, strong) NSMutableArray *jsonData;

@end

FirstViewController.m

@synthesize jsonData;

NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=coredata"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
self.json = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.secondJSON = [self jsonData];
[[self navigationController] pushViewController:secondViewController animated:YES];

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
    NSMutableArray *secondJSON;
}

@property (nonatomic, strong) NSMutableArray *secondJSON;

@end

SecondViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@", [self secondJSON]);
}

输出

2013-02-03 00:16:17.749 Subscription[24669:c07] (null)

我错过了什么吗?

6 个答案:

答案 0 :(得分:2)

也许是[[[UIApplication sharedApplication] delegate] json]

(并且, 在应用程序启动时不在主线程上发送同步请求!

答案 1 :(得分:2)

第一件事

我正在使用ARC然后更改

@property (nonatomic, retain) NSMutableArray *json;

@property (nonatomic, strong) NSMutableArray *json;

因为保留使你的弱引用。

在viewcontroller中使用

进行访问
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//appDelegate.json //Your Array

为什么不直接在viewController中实现此方法,AppDelegate类应该尽可能干净。

希望这可以帮助你..

答案 2 :(得分:1)

您可以在所需视图中创建Application Delegate实例并调用其对象。

AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSMutableArray *arMyArray = [NSMutableArray arrayWithArray:[appDelegate.json mutableCopy]];

答案 3 :(得分:1)

试试这个,

     AppDelegate *appobj =  [[UIApplication sharedApplication] delegate];
     NSLog(@"%@",appobj.json);

答案 4 :(得分:1)

在你想要访问json数组的视图控制器中,像这样制作appdelegate的实例:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

然后通过使用这个实例来访问json数组。

 NSMutableArray *arMyArray = [[NSMutableArray alloc]initWithAaray:appdelegate.json];

答案 5 :(得分:1)

NSMutableArray *listOfData中创建SecondViewController,例如

#import <UIKit/UIKit.h>

    @interface SecondViewController : UIViewController

    @property (nonatomic, strong) NSMutableArray *listOfData;

    @end

并在导航时将NSMutableArray *jsonData;传递给SecondViewController  (在SecondViewController的创建对象),如

此代码写入 FirstViewController

 SecondViewController *lvController = [[SecondViewController alloc] init];
 lvController.listOfData = self.jsonData;
 [self.navigationController pushViewController:lvController animated:YES];

并使用viewDidLoad的{​​{1}}方法进行检查,例如

SecondViewController

此代码可能对您有所帮助:)