【发布时间】:2015-06-16 21:29:37
【问题描述】:
我使用 SpriteKit(GameScene 和 GameViewController)和 Objective-C 创建了一个游戏。那里的一切都很好。 在同一个应用程序中,我在情节提要中创建了一个 UIViewController 和第二个使用 CollectionView 的 viewController。我让 collectionView 加载数组。
我希望能够点击 collectionView Cell 并让它打开 gameView。我能做到的。不过,我想将一些信息从 collectionView 传递给 GameScene,这样我就可以加载背景图像和其他信息来为每个 collectionViewCell 个性化 GameScene。
这是我的 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 所以我不能这样做。
我尝试过使用 segue,但信息仍然无法从另一个传递到另一个。
我应该怎么做?
【问题讨论】:
标签: uiviewcontroller sprite-kit uicollectionview