1

我是 Objective 和 iPad 应用程序的新手。我试图显示一个我已经在单独的类/视图控制器中使用的数组。我试图访问的数组在“MainMenuScreen.m”中,我想在另一个名为“ScoresScreen.m”的类中使用该数组。我在网上搜索过,它似乎回来了一个叫做 Segue 的东西?我正在浏览示例,但我不断收到错误,不知道该怎么做。当然必须有一种简单的方法来从不同的类中获取一个数组并显示它?我确实尝试在“ScoresScreen.m”开头执行此操作 - 导入“MainMenuScreen.m”,但在尝试从中调用数组时出现错误。我所有的代码都在工作,我只需要从另一个类中获取数组并插入我的代码。我不指望你为我做代码,但请告诉我如何使用不同类的数组?先感谢您


这是 ScoresScreen.h 的代码

#import <UIKit/UIKit.h>


@interface ScoresScreen : UIViewController<UITableViewDataSource, UITableViewDelegate>{
     NSMutableArray *scoresArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

这是 ScoresScreen.m 代码 =

#import "ScoresScreen.h"
#import "MainMenuScreen.h"




@interface ScoresScreen ()

@end

@implementation ScoresScreen

NSMutableArray *TestScoresArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{


    TestScoresArray = [[NSMutableArray alloc] init];//test data

    [TestScoresArray addObject: [NSMutableArray arrayWithObjects:@"Name : Bob",@" | Score : 134", nil]];//testData
    [TestScoresArray addObject: [NSMutableArray arrayWithObjects:@"Name : Roger",@" | Score : 12",nil]];//testData
    [TestScoresArray addObject: [NSMutableArray arrayWithObjects:@"Name : Ben",@"| Score : 34",nil]];//testData
    [super viewDidLoad];



    for(NSMutableArray *m in TestScoresArray)
    {
        NSLog(@"%@%@", m[0],m[1]);
   }

}

- (void)addNewScore
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}
-  (NSInteger)tableView:(UITableView *)tableView
  numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }



NSLog(@"%@",scoresArray);

    NSString *continent;
   NSMutableArray *country = [scoresArray objectAtIndex:indexPath.row];
    continent =[NSString stringWithFormat:@"%@ %@",[country objectAtIndex:0],[country objectAtIndex:1]];
cell.textLabel.text = continent;

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 return cell;
}


- (void)viewDidUnload {
    [self setTableView:nil];
    [super viewDidUnload];
}
@end

这是 MainMenuScreen.h 的代码

#import <UIKit/UIKit.h>

@interface MainMenuScreen : UIViewController

- (IBAction)PlayGameBT:(id)sender;

- (IBAction)ViewScoresBT:(id)sender;

-(void)AddNewScore :(NSString *)userName :(int)score;//for PlayGame see .m

@end

这是 MainMenuScreen.m 的代码

#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"

@interface MainMenuScreen ()

@end

@implementation MainMenuScreen

//---------------------------------------------------------------------------------

//------------------------------------Variables------------------------------------

PlayGame *playGame;//PlayGameObject

NSMutableArray *gameDataArray;//Game Data Array (A array storying multable ImageTest Objects)

NSMutableArray *scoresArray;//<-------This need to go to view scores

//---------------------------------------------------------------------------------

//----------------------------------Constructors-----------------------------------

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    scoresArray = [[NSMutableArray alloc] init];

    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Bob",[NSNumber numberWithInt:134],nil]];//testData
    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Roger",[NSNumber numberWithInt:12],nil]];//testData
    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Ben",[NSNumber numberWithInt:34],nil]];//testData

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

//---------------------------------------------------------------------------------

//----------------------------------Add New Score----------------------------------
//a setter Method so Play Game can add the user name and score to the scoresArray
-(void)AddNewScore :(NSString *)userName :(int)score
{
    //adds user name and score to the array
    if (scoresArray != nil)
    {
        /*NSMutableArray *nameAndScore = [[NSMutableArray alloc] init];
        [nameAndScore addObject:userName];
        [nameAndScore addObject: [NSNumber numberWithInt:score]];//int needs to be converted before storing in an array
        [scoresArray addObject:nameAndScore];*/

        [scoresArray addObject: [NSMutableArray arrayWithObjects:userName,[NSNumber numberWithInt:score],nil]];


        for(NSMutableArray *m in scoresArray)
        {
            NSLog(@"%@%@", m[0],m[1]);//prints out the array in logs to make sure it is working
        }
    }
}
4

3 回答 3

0

您可以通过在第二个 ScoresScreen.h 文件中定义一个数组属性来做到这一点,例如:

@interface ScoresScreen : UIViewController {
   NSArray *array;
}
@property(nonatomic, assign)NSArray *array;

在 .m 文件中合成它就像

@synthesize array;

现在在 MainMenuScreen.m 中分配它

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   ScoresScreen *controller = [segue destinationViewController];
   controller.array = yourArray;
}



编辑
.h

#import <UIKit/UIKit.h>


@interface ScoresScreen : UIViewController<UITableViewDataSource, UITableViewDelegate>{
     NSMutableArray *scoresArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) NSMutableArray *scoresArray; //Added this line

@end

.m

#import "ScoresScreen.h"
#import "MainMenuScreen.h"

@interface ScoresScreen ()

@end

@implementation ScoresScreen
@synthesize scoresArray; //Added this line
NSMutableArray *TestScoresArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   ...
....
....

你妈

#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"

@interface MainMenuScreen ()

@end

@implementation MainMenuScreen

//---------------------------------------------------------------------------------

//------------------------------------Variables------------------------------------

PlayGame *playGame;//PlayGameObject

NSMutableArray *gameDataArray;//Game Data Array (A array storying multable ImageTest Objects)

NSMutableArray *scoresArray;//<-------This need to go to view scores

//---------------------------------------------------------------------------------

//----------------------------------Constructors-----------------------------------

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    scoresArray = [[NSMutableArray alloc] init];

    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Bob",[NSNumber numberWithInt:134],nil]];//testData
    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Roger",[NSNumber numberWithInt:12],nil]];//testData
    [scoresArray addObject: [NSMutableArray arrayWithObjects:@"Ben",[NSNumber numberWithInt:34],nil]];//testData

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

//---------------------------------------------------------------------------------

//----------------------------------Add New Score----------------------------------
//a setter Method so Play Game can add the user name and score to the scoresArray
-(void)AddNewScore :(NSString *)userName :(int)score
{
    //adds user name and score to the array
    if (scoresArray != nil)
    {
        /*NSMutableArray *nameAndScore = [[NSMutableArray alloc] init];
        [nameAndScore addObject:userName];
        [nameAndScore addObject: [NSNumber numberWithInt:score]];//int needs to be converted before storing in an array
        [scoresArray addObject:nameAndScore];*/

        [scoresArray addObject: [NSMutableArray arrayWithObjects:userName,[NSNumber numberWithInt:score],nil]];


        for(NSMutableArray *m in scoresArray)
        {
            NSLog(@"%@%@", m[0],m[1]);//prints out the array in logs to make sure it is working
        }
    }
}


    //added this method
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
       ScoresScreen *controller = [segue destinationViewController];
       controller.array = yourArray;
    }
于 2012-12-11T15:23:14.917 回答
0

你可以用两种或三种不同的方式来做。除了瑞安已经评论过的内容。

  1. 您可以在创建 ScoresScreen 时但在呈现 ScoresScreen 之前从 MainMenuScreen 传递它。

  2. 您可以使 Array 成为 MainMenuScreen 的属性,该属性可以从导入它的任何类中检索。

  3. 您可以有一个单独的 DataManager 类(通常是一个单例类)来管理数组,并且两个类都可以从那里获取数组。

另外:导入时,您应该导入 .h 文件,而不是像您在帖子中输入的 .m 文件,不确定这是您的代码中的拼写错误还是错误。如果您在 MainMenuScreen 中有一个方法或属性将返回数组,则需要在头文件 (.h) 中声明它,通过该头文件导入它的类知道它的存在。

于 2012-12-11T15:21:25.213 回答
0

将数组移动到您的应用委托。要从任何类访问应用程序委托,请执行以下操作:

#import "NameOfYourAppDelegate.h"

在文件的顶部,和

NameOfYourAppDelegate *appDelegate = [UIApplication sharedApplication].delegate

你想在哪里使用它。

于 2012-12-11T16:34:54.490 回答