2014-09-02 96 views
1

我有两个类。首先下载数据映像并将它们逐个添加到阵列中。当它完成后,所有16个人都会将视图切换到第二个视图。我的问题在于如何从第二课的第一课访问数组?从不同的视图控制器访问阵列

下面是代码:

first.h

@interface first{ 
    NSMutableArray *imagesArray; 
} 
@property(nonatomic,retain)NSMutableArray *imagesArray; 

(阵列在.m文件synthetized)

second.h

#import"second"; 
#import"first.h" 

second.m

-(void) viewdidload { 
    first *another = [[another alloc] init]; 
    label.text = [NSString stringWithFromat:@"%i",[another.imagesArray count]]; 
    imageView.image = [another.imagesArray objectAtIndex:0]; 
} 

label表示0和imageView说明不了什么。有任何想法吗?

+0

你可以创建全局数组或者在** second.h **中定义相同的@property(nonatomic,retain)NSMutableArray * imagesArray;'当你从First到Second时,传递该数组。所以你不需要分配第一个视图。 – 2014-09-02 04:19:59

+0

http://stackoverflow.com/questions/6678097/how-to-pass-an-array-from-one-view-controller-to-another和http://stackoverflow.com/questions/15789163/add-objects从另一个视图控制器到阵列可能会有所帮助 – 2014-09-02 04:31:50

回答

0

请在Objective-C

得到很好的理解面向对象特性&对象沟通不要创建first类的新实例。这将创建一个新的内存对象first

您必须访问在现有的first类中创建的现有的imagesArray类对象。

要做到这一点,在你second.m声明NSMutableArray财产weakfirst.m类,而你分配second.m分配给它。

例子:

@interface second { 

    NSMutableArray *parentImagesArray; 
} 

@property(nonatomic, weak)NSMutableArray *parentImagesArray; 

first.m

您将有类似的代码在你first.m文件

second *secondView = [[second alloc] init]; 
secondView.parentImagesArray = self.imagesArray; //assign the weak property. 

second.m

label.text = [NSString stringWithFromat:@"%i",[self.parentImagesArray count]]; 
imageView.image = parentImagesArray objectAtIndex:0]; 
0

没有达到你的目标双向...

1)FIRST(从第一控制器到第二个控制器)

化妆数组对象在第二或者Controller.h 然后

传递数组

从其中呈现第一控制器第二控制器

second.array_object = first.array_name; 

在这样你得到了第二个控制器中的数组...

2)声明数组作为全地球

Declare array before import statement. 
in first.h file 

NSMutableArray *arr1; 
#import<UIKit/UIKit.h> 

现在无论何时何地你想使用的.h文件中声明该数组该数组进口并直接使用它而不会产生任何对象...

希望这可以帮助你...

相关问题