【问题标题】:How to copy an array to appdelegate array and then retrieve it again elsewhere?如何将数组复制到 appdelegate 数组,然后在其他地方再次检索它?
【发布时间】:2011-10-13 14:57:49
【问题描述】:

我想将 url 数组存储在我的 appdelegate 数组中,即 myMutableArray 中的 logoArray,然后在其他视图控制器中使用它,但我可以按原样复制我正在做浅拷贝,我也尝试过其他方法,例如 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);
    }
}

【问题讨论】:

  • 其实我想在 ImageView 上动态显示图像...任何帮助都会很棒...

标签: iphone


【解决方案1】:

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

这个帖子可能会有所帮助: iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2015-06-05
    • 1970-01-01
    相关资源
    最近更新 更多