将信息从 collectionView 传递到 gameScene

问题描述:

我使用 SpriteKit(GameScene 和 GameViewController)和 Objective-C 创建了一个游戏.那里一切正常.在同一个应用程序中,我在使用 CollectionView 的故事板中创建了一个 UIViewController 和第二个 viewController.我有 collectionView 加载数组.

I have created a game using SpriteKit (GameScene and GameViewController) and Objective-C. Everything there works fine. Within the same App I have created a UIViewController and a second viewController in storyboard that uses CollectionView. I have the collectionView loading the array.

我希望能够点击 collectionView Cell 并让它打开游戏视图.我能做的.但是,我想将 collectionView 中的一些信息传递给 GameScene,以便我可以加载背景图像和其他信息,以便为每个 collectionViewCell 个性化 GameScene.

I want to be able to tap on a collectionView Cell and have it open the gameView. Which I can do. However I would like to pass some information from the collectionView to the GameScene so I can load the background image and other information to individualize the GameScene for each collectionViewCell.

这是我的 collectionView 代码

Here is my code for the collectionView

#import "collectionViewController.h"
#import "GameScene.h"
#import "GameViewController.h"
@implementation collectionViewController
@synthesize animalArray,theImage,StringPicture,myCell;

-(void) viewDidLoad {
    [super viewDidLoad];
   animalArray = [NSArray arrayWithObjects:@"Cat.png",
                     @"image1.png",
                     @"image2.png",
                     @"image3.png",
                    ,nil];
}

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}




-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return animalArray.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    UIImage *images;
    long row = [indexPath row];
    images = [UIImage imageNamed:animalArray[row]];

    myCell.image1.image = images;

    return myCell;
}




-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"indexPath.row:%ld",(long)indexPath.row);


    if (indexPath.row==1) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];
       vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        NSLog(@"mycell.image1.image:%@",myCell.image1.image);

        [self presentViewController:vc animated:YES completion:NULL];


    }

    if (indexPath.row==2) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GameSceneOne"];
        vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:vc animated:YES completion:NULL];

    }
}

我尝试将 GameScene.image 分配给 vc.image,但一个是 GameView,另一个是 UIViewController,所以我不能这样做.

I have tried assigning the GameScene.image to the vc.image but one is a GameView and the other is a UIViewController so I can't do that.

我尝试过使用 segue,但仍然无法将信息传递给另一个.

I have tried using a segue but still the information can't be passed from on to the other.

我该怎么做?

有几种方法可以做到这一点.其中之一是使用 NSUserDefaults.

Couple of ways to do this. One of them is using NSUserDefaults.

要将数据存储在 NSUserDefaults 中,请执行以下操作:

To store data in NSUserDefaults, do this:

// object can be almost anything. Strings, arrays, dictionaries...

NSString *object = @"WellDone";

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:@"Hamburger"];
[defaults synchronize];

要读取 NSUserDefaults,请执行以下操作:

To read NSUserDefaults, do this:

NSString *myString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Hamburger"];

关键在于您使用的密钥与创建时相同.

The key being that you use the same key as when you created it.

当然,请阅读文档(废话,废话,废话).

And of course, read the docs (blah,blah, blah).