2014-09-02 122 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文件中聲明該數組該數組進口並直接使用它而不會產生任何對象...

希望這可以幫助你...

相關問題