如何将数组复制到appdelegate数组,然后在其他地方再次检索它?

时间:2011-10-13 14:57:50

标签: iphone

我想在我的appdelegate数组中存储url数组,即 logoArray 来自 myMutableArray ,然后在其他viewcontroller中使用它,但我可以复制,因为可能是我正在做浅拷贝,我尝试过像 initwithArray:copyItems 这样的方法。

代码: -

@class FirstViewController;

@interface AppDelegate_iPhone : NSObject <UIApplicationDelegate> {
    UIWindow *window;

    FirstViewController *viewController;

    NSMutableArray *logoArray;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *logoArray;

@end


// NO initialization of logoArra is done in .M file    


@class AppDelegate_iPhone;    
@interface FirstViewController : UIViewController {

    NSMutableArray *array;

    NSString *logoString;
    AppDelegate_iPhone *appDelegate;

}



@end



@implementation FirstViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    int x=5,y=10;

    UIApplication *app = [UIApplication sharedApplication];
    appDelegate=app.delegate;

    NSLog(@" Array ====== %d",[appDelegate.logoArray count]);

    array = [[NSMutableArray alloc]initWithArray:appDelegate.logoArray];

    NSLog(@"array at 0 ===== %@",[array objectAtIndex:0]);


    for (int i=0; i<[array count]; i++) {

        logoString = [array objectAtIndex:i];
        NSLog(@"%@",logoString);
        UIImage *imageFromUrl = [UIImage imageWithContentsOfFile:[NSURL fileURLWithPath:logoString]];



        UIImageView *imgView = [[UIImageView alloc] initWithImage:imageFromUrl];
        [imgView setFrame:CGRectMake(x, y, 196, 90)];
        [self.view addSubview:imgView];


    //  UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage)];
    //  [imgView addGestureRecognizer:tgr];
    //  [tgr release];

        //Do the rest of your operations here, don't forget to release the UIImageView
        x = x + 200;
    //  [imgView release];

    }


}





@class Litofinter,AppDelegate_iPhone;

@interface ParsingViewController : NSObject<NSXMLParserDelegate> {

    NSString *myString;
    NSMutableArray *myMutableArray;
    Litofinter *obj;
    NSString *currentElement;

    AppDelegate_iPhone *appDelegate;


}

@property(nonatomic, retain) NSString *myString;
@property(nonatomic, retain) NSArray *myMutableArray;

@end





#import "ParsingViewController.h"
#import "Litofinter.h"
#import "AppDelegate_iPhone.h"


@implementation ParsingViewController

@synthesize myMutableArray, myString;

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    myMutableArray = [[NSMutableArray alloc]init];

}


// I have parsed here my XML and array gets stored in myMutableArray


- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    UIApplication *app = [UIApplication sharedApplication];
    appDelegate=app.delegate;

    appDelegate.logoArray = [[NSMutableArray alloc]initWithArray:myMutableArray];

//  NSLog(@"appDelegate.logoArray count %d",[appDelegate.logoArray count]);

    for (Litofinter *lito in appDelegate.logoArray) {
        NSLog(@"Array Elements :----- %@",lito.cLogo);
    }
}

1 个答案:

答案 0 :(得分:1)

我个人不会在viewcontroller中创建一个数组,然后将其存储在appdelegate中。我更倾向于为数据创建一个模型(一个获取并存储数据并将其提供给视图控制器的类)。

此主题可能会有所帮助: iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable