我是 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
}
}
}