0

我有一个 NSMutableArray,我想在另一个视图中使用它的数据。查了一下发现我们可以使用indexOfObject来读取数据,但是我的NSMutableArray有多个值,我不知道如何从NSMutableArray中获取特定的字段。

任何帮助将不胜感激。

我目前被困在我的 IBAction onStartPressed 上。在线

    self.clueHint.text = ds[0].initialClue;
//or
    self.clueHint.text = ds objectAtIndex:0 initialClue;

我展示了我的其他文件和涉及的代码。

线索数据.h #import

@interface ClueData : NSObject
@property(nonatomic, strong) NSString * clueName;
@property(nonatomic, strong) NSString * initialClue;
@property(nonatomic, strong) NSString * clueHint1;
@property(nonatomic, strong) NSString * clueHint2;

@property(nonatomic, strong) NSString * clueAnswer;
@property(nonatomic, strong) UIImage * clueImage;
@end

线索数据DAO.h

#import <Foundation/Foundation.h>
#import "ClueData.h"
@interface ClueDataDAO : NSObject
@property(strong, nonatomic) NSMutableArray * clueDataArray;

-(NSMutableArray *) PopulateDataSource;
@end

线索数据DAO.m

#import "ClueDataDAO.h"

@implementation ClueDataDAO
@synthesize clueDataArray;
-(NSMutableArray *) PopulateDataSource
{
    clueDataArray = [[NSMutableArray alloc]init];
     ClueData * sampleData = [[ClueData alloc]init];

    sampleData.clueName = @"Tropical Fruit Tree";
    sampleData.initialClue = @"Where all the Tropical trees are planted.";
    sampleData.clueHint1 = @"It is located at Area 10";
    sampleData.clueHint2 = @"It could be found near Eco Lake";
    sampleData.clueAnswer = @"Fruit tree collection";
    NSString * file=[[NSBundle mainBundle]pathForResource:@"FruitTreeCollection" ofType:@"jpg"];
    sampleData.clueImage = [UIImage imageWithContentsOfFile:file];
    [clueDataArray addObject:sampleData];
    sampleData=nil;

    return clueDataArray;
}
@end

游戏页面视图控制器.M

- (void)viewDidLoad
{
    [super viewDidLoad];
    daoDS = [[ClueDataDAO alloc]init];
    self.ds = daoDS.PopulateDataSource; //load pre-build NSMutableArray
    self.answer.delegate = self;
    // Do any additional setup after loading the view.
}

- (IBAction)onStartPressed:(id)sender {

    //initiate load object
// stuck at here :(

    ClueData * currentClue = [ds objectAtIndex:0]; // app got terminate at this line of code
self.clueHint.text = [currentClue initialClue];


}
4

1 回答 1

1

在 IBAction 中,首先将其转换/保存到正确的 var,例如

ClueData * sampleData = [ds objectAtIndex:0];

然后 self.clueHint.text= [sampleData initialClue];

例如

于 2012-10-14T11:30:57.947 回答