2015-02-10 209 views
-1

我有2个类,即一个是“MainClass”,另一个是“StartView”类。我想创建MainClass的对象数组到StartView类。热的创建类的对象数组到另一个类

这里我创建了10个MainClass对象并添加到一个可变数组中。 该数组也是MainClass.Its的一部分,它很难访问它们。 有什么办法直接创建对象数组。我想知道如何访问以及如何在另一个类中创建对象数组。代码如下...

//MainClass.h 
#import <Foundation/Foundation.h> 
#import "StartView.h" 
#import "TableViewController.h" 
@interface MainClass : NSObject 

    @property(nonatomic, strong) NSString *que; 
    @property(nonatomic, strong) NSString *img; 
    @property(nonatomic, strong) NSArray *option; 
    @property(nonatomic, strong) NSMutableArray *nms; 
    -(void)ObjectsAssignment; 

@end 

//MainClass.m 
    -(void)ObjectsAssignment 
    { 
     nms=[[NSMutableArray alloc]init]; 

     MainClass *mc1=[[MainClass alloc]init]; 
     [email protected]"Who invented Microprocessor ?"; 
     [email protected]"SuperComputer.jpg"; 
     mc1.option=[[NSArray alloc]initWithObjects:@"Joseph 
     Jacquard",@"Herman H 
     Goldstein",@"Marcian E Huff",@"George Boole",nil]; 
     [nms addObject:mc1]; 


    MainClass *mc2=[[MainClass alloc]init]; 
    [email protected]".INI extention refers to which kind of file ? "; 
    [email protected]"SuperComputer.jpg"; 
    mc2.option=[[NSArray alloc]initWithObjects:@"Joseph Jacquard", 
    @"Herman H Goldstein",@"Marcian E Huff",@"George Boole",nil]; 
    [nms addObject:mc2]; 
} 
@end 
+1

这里是你的代码? – rishi 2015-02-10 11:39:08

+0

请注意,这与'xcode IDE'无关,所以请不要使用该标签。 – Popeye 2015-02-10 13:18:36

+0

[视图控制器之间传递数据](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – 2015-02-10 13:30:20

回答

0

在StartView中创建一个属性,您可以将其分配给该数组。 StartView和您的其他代码然后共享指向同一个数组的指针,并且都可以访问它。

在StartView:

@property NSMutableArray *myArrayReference; 

别处

NSMutableArray *theArray=[[NSMutableArray alloc] init]; 
for (int i=0;i<10;i++){ 
    MainClass *instance=[[MainClass alloc] init]; 
    [theArray addObject:instance] 
} 

// Pass this array to other object. 
StartView *startView=[[StartView alloc] init]; 
[startView setMyArrayReference:theArray]; 
+0

如何访问该对象内的变量 – 2015-02-10 13:35:39

+0

在StartView中:'self。 myArrayReference [<插入索引>]' – 2015-02-10 13:37:43

相关问题